zip 解密加密 ZipUtils
阿新 • • 發佈:2019-01-14
package com.topsec.ws.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.Deflater; import java.util.zip.Inflater; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipUtils { /** * * @param inputByte * 待解壓縮的位元組陣列 * @return 解壓縮後的位元組陣列 * @throws IOException */ public static byte[] uncompress(byte[] inputByte) throws IOException { int len = 0; Inflater infl = new Inflater(); infl.setInput(inputByte); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] outByte = new byte[1024]; try { while (!infl.finished()) { // 解壓縮並將解壓縮後的內容輸出到位元組輸出流bos中 len = infl.inflate(outByte); if (len == 0) { break; } bos.write(outByte, 0, len); } infl.end(); } catch (Exception e) { // } finally { bos.close(); } return bos.toByteArray(); } /** * 壓縮. * * @param inputByte * 待壓縮的位元組陣列 * @return 壓縮後的資料 * @throws IOException */ public static byte[] compress(byte[] inputByte) throws IOException { int len = 0; Deflater defl = new Deflater(1);//這裡可以設定壓縮的比,看實際情況進行修改 defl.setInput(inputByte); defl.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] outputByte = new byte[1024]; try { while (!defl.finished()) { // 壓縮並將壓縮後的內容輸出到位元組輸出流bos中 len = defl.deflate(outputByte); bos.write(outputByte, 0, len); } defl.end(); } finally { bos.close(); } return bos.toByteArray(); } /** * @param sourceFileName 原始檔 * @param zipFileName 壓縮後文件 * @throws Exception */ public static void zip(String sourceFileName,String zipFileName) throws Exception { //建立zip輸出流 ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName)); //建立緩衝輸出流 BufferedOutputStream bos = new BufferedOutputStream(out); File sourceFile = new File(sourceFileName); //呼叫函式 compress(out,bos,sourceFile,sourceFile.getName()); bos.close(); out.close(); } public static void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception { //如果路徑為目錄(資料夾) if(sourceFile.isDirectory()) { //取出資料夾中的檔案(或子資料夾) File[] flist = sourceFile.listFiles(); if(flist.length==0)//如果資料夾為空,則只需在目的地zip檔案中寫入一個目錄進入點 { System.out.println(base + File.pathSeparator); out.putNextEntry(new ZipEntry(base + File.pathSeparator)); } else//如果資料夾不為空,則遞迴呼叫compress,資料夾中的每一個檔案(或資料夾)進行壓縮 { for(int i=0;i<flist.length;i++) { compress(out,bos,flist[i],base+File.separator+flist[i].getName()); } } } else//如果不是目錄(資料夾),即為檔案,則先寫入目錄進入點,之後將檔案寫入zip檔案中 { out.putNextEntry( new ZipEntry(base) ); FileInputStream fos = new FileInputStream(sourceFile); BufferedInputStream bis = new BufferedInputStream(fos); int tag; System.out.println(base); //將原始檔寫入到zip檔案中 while((tag=bis.read())!=-1) { bos.write(tag); } bis.close(); fos.close(); } } /** * zip解壓 * @param srcFile zip原始檔 * @param destDirPath 解壓後的目標資料夾 * @throws RuntimeException 解壓失敗會丟擲執行時異常 */ public static void unZip(File srcFile, String destDirPath) throws RuntimeException { long start = System.currentTimeMillis(); // 判斷原始檔是否存在 if (!srcFile.exists()) { throw new RuntimeException(srcFile.getPath() + "所指檔案不存在"); } // 開始解壓 ZipFile zipFile = null; try { zipFile = new ZipFile(srcFile); Enumeration<?> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); // 如果是資料夾,就建立個資料夾 if (entry.isDirectory()) { String dirPath = destDirPath + "/" + entry.getName(); File dir = new File(dirPath); dir.mkdirs(); } else { // 如果是檔案,就先建立一個檔案,然後用io流把內容copy過去 File targetFile = new File(destDirPath + "/" + entry.getName()); // 保證這個檔案的父資料夾必須要存在 if(!targetFile.getParentFile().exists()){ targetFile.getParentFile().mkdirs(); } targetFile.createNewFile(); // 將壓縮檔案內容寫入到這個檔案中 InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile); int len; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } // 關流順序,先開啟的後關閉 fos.close(); is.close(); } } long end = System.currentTimeMillis(); System.out.println("解壓完成,耗時:" + (end - start) +" ms"); } catch (Exception e) { throw new RuntimeException("unzip error from ZipUtils", e); } finally { if(zipFile != null){ try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { try { zip("D:\\aa\\test.txt","D:\\aa\\test.zip"); unZip(new File("D:/bb/bb.zip"),"D:/bb/bb"); } catch (Exception e) { System.out.println(e.getLocalizedMessage()); } } }