日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

压缩解压zip文件包

發(fā)布時間:2024/1/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 压缩解压zip文件包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import java.io.*;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tools.zip.*; import java.util.Enumeration;

?

public class AntZip {// 日志對象private static Log logger = LogFactory.getLog(AntZip.class);/*** <壓縮指定的文件夾>* <該方法只能壓縮文件夾>* @param zipDirectory 需要壓縮的文件夾全路徑* @param destFile 壓縮后的文件存放路徑,包括壓縮后的文件名* @see [類、類#方法、類#成員]*/public static void doZip(String zipDirectory, String destFile){File zipDir = new File(zipDirectory);try{ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));handleDir(zipDir, zipOut);zipOut.close();}catch (IOException e){logger.error(Global.LOG_EXCEPTION_NAME, e);}}/** * <由doZip調用,遞歸完成目錄文件讀取>* <功能詳細描述>* @param dir 文件* @param zipOut 壓縮輸出對象* @throws IOException* @see [類、類#方法、類#成員]*/private static void handleDir(File dir, ZipOutputStream zipOut)throws IOException{FileInputStream fileIn;File[] files;files = dir.listFiles();if (files.length == 0){//如果目錄為空,則單獨創(chuàng)建之. //ZipEntry的isDirectory()方法中,目錄以"/"結尾. zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));zipOut.closeEntry();}else{//如果目錄不為空,則分別處理目錄和文件. for (File fileName : files){if (fileName.isDirectory()){handleDir(fileName, zipOut);}else{fileIn = new FileInputStream(fileName);String path = fileName.getPath().substring(fileName.getPath().lastIndexOf("\\") + 1);zipOut.putNextEntry(new ZipEntry(path));byte[] buf = new byte[1024];int readedBytes = 0;while ((readedBytes = fileIn.read(buf)) > 0){zipOut.write(buf, 0, readedBytes);}fileIn.close();zipOut.closeEntry();}}}}/*** <解壓指定zip文件>* <解壓指定zip壓縮包到指定目錄>* @param unZipfileName 壓縮包* @param destDir 目標目錄* @see [類、類#方法、類#成員]*/@SuppressWarnings("rawtypes")public static void unZip(String unZipfileName, String destDir){//unZipfileName需要解壓的zip文件名 FileOutputStream fileOut;File file;InputStream inputStream;try{ZipFile zipFile = new ZipFile(unZipfileName);for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();){ZipEntry entry = (ZipEntry)entries.nextElement();file = new File(entry.getName());if (entry.isDirectory()){file.mkdirs();}else{//如果指定文件的目錄不存在,則創(chuàng)建之. String path = destDir + "/" + file.getParent();File parent = new File(path);if (!parent.exists()){parent.mkdirs();}inputStream = zipFile.getInputStream(entry);fileOut = new FileOutputStream(destDir + "/" + file);byte[] buf = new byte[1024];int readedBytes = 0;while ((readedBytes = inputStream.read(buf)) > 0){fileOut.write(buf, 0, readedBytes);}fileOut.close();inputStream.close();}}zipFile.close();}catch (IOException e){logger.error(Global.LOG_EXCEPTION_NAME, e);}} }

?

總結

以上是生活随笔為你收集整理的压缩解压zip文件包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。