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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java实现Zip文件解压

發(fā)布時間:2025/4/5 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现Zip文件解压 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

##1. 兩種java實現(xiàn)zip文件解壓方式

  • 使用JDK的原生類java.util.zip,上代碼:
import java.util.zip.*; import java.io.*;public class UnzipTest {public static void main(String[] args) {if (args.length != 1) {System.out.println("請輸入正確參數(shù):java UnzipTest 需解壓的文件(e.g. d:/test.zip)");} else {Unzip unzip = new Unzip();if (unzip.unzip(args[0])) {System.out.println("文件解壓成功。");} else {System.out.println("文件解壓失敗。");}} } }class Unzip {public Unzip() {}/** @param srcZipFile 需解壓的文件名* @return 如果解壓成功返回true*/public boolean unzip(String srcZipFile) {boolean isSuccessful = true;try {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcZipFile));ZipInputStream zis = new ZipInputStream(bis);BufferedOutputStream bos = null;//byte[] b = new byte[1024];ZipEntry entry = null;while ((entry=zis.getNextEntry()) != null) {String entryName = entry.getName();bos = new BufferedOutputStream(new FileOutputStream("d:/" + entryName));int b = 0;while ((b = zis.read()) != -1) {bos.write(b);}bos.flush();bos.close();}zis.close();} catch (IOException e) {isSuccessful = false;}return isSuccessful;} }

這種解壓方式會出現(xiàn)中文文件名亂碼的問題。

  • 另一種實現(xiàn)zip文件解壓的方式,使用 ant.jar 的org.apache.tools.zip包,上代碼:
import java.io.*; import java.util.Enumeration; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expand; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.mockito.internal.util.io.IOUtil;public class Unzip {private static final String ENCODE = "UTF-8";/** @param zipDir目標文件存放地,zipFile帶解壓文件* @return 如果解壓成功返回true*/public static boolean unzip(String zipDir, String zipFile) {ZipFile zfile = null;InputStream is = null;OutputStream os = null;try{zfile = new ZipFile(zipFile);Enumeration<?> zList = zfile.getEntries();ZipEntry ze = null;byte[] buf = new byte[1024];while (zList.hasMoreElements()){ze = (ZipEntry) zList.nextElement();if (ze.isDirectory()){/*// 獲得當前時間DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 轉(zhuǎn)換為字符串String formatDate = format.format(new Date());// 隨機生成文件編號int random = new Random().nextInt(10000);*/File f = new File(zipDir + ze.getName());f.mkdir();continue;}os = new BufferedOutputStream(new FileOutputStream(getRealFileName(zipDir, ze.getName())));is = new BufferedInputStream(zfile.getInputStream(ze));int readLen = 0;while ((readLen = is.read(buf, 0, 1024)) != -1){os.write(buf, 0, readLen);}IOUtil.closeQuietly(is);IOUtil.closeQuietly(os);}zfile.close();return true;}catch (IOException e){e.printStackTrace();}finally{IOUtil.closeQuietly(is);IOUtil.closeQuietly(os);try{if (null != zfile){zfile.close();}}catch (IOException ex){// ignoreSystem.out.println(ex);}}return false;}/*** 給定根目錄,返回一個相對路徑所對應(yīng)的實際文件名.** @param baseDir* 指定根目錄* @param absFileName* 相對路徑名,來自于ZipEntry中的name* @return java.io.File 實際的文件*/public static File getRealFileName(String baseDir, String absFileName){String[] dirs = absFileName.split("/");File ret = new File(baseDir);if (dirs.length > 1){for (int i = 0; i < dirs.length - 1; i++){ret = new File(ret, dirs[i]);}if (!ret.exists())ret.mkdirs();ret = new File(ret, dirs[dirs.length - 1]);return ret;}ret = new File(ret, dirs[dirs.length - 1]);return ret;} }

ant.jar的maven依賴

<dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.10.1</version> </dependency>

第二種方法可以避免中文文件名亂碼的問題。

轉(zhuǎn)載于:https://my.oschina.net/799835984/blog/1476570

總結(jié)

以上是生活随笔為你收集整理的Java实现Zip文件解压的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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