生活随笔
收集整理的這篇文章主要介紹了
Java实现Zip文件解压
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
##1. 兩種java實現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("請輸入正確參數: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;}
}
這種解壓方式會出現中文文件名亂碼的問題。
- 另一種實現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");// 轉換為字符串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;}/*** 給定根目錄,返回一個相對路徑所對應的實際文件名.** @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>
第二種方法可以避免中文文件名亂碼的問題。
轉載于:https://my.oschina.net/799835984/blog/1476570
總結
以上是生活随笔為你收集整理的Java实现Zip文件解压的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。