java 解压压缩包
1.解压压缩包,目前zip的压缩包可以解压,rar后缀的最新版本的不支持解压,官方目前好像也没给出好办法。
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; /** * 解压缩 * * @param zipFilePath * 压缩包路径 * @param fileSavePath * 解压路径 * @param isDelete * 是否删除源文件 * @throws Exception */ public static void unZip(String sZipFilePath, String sFileSavePath, boolean isDelete) throws Exception { try { (new File(sFileSavePath)).mkdirs(); File f = new File(sZipFilePath); if ((!f.exists()) && (f.length() <= 0)) { throw new Exception("要解压的文件不存在!"); } ZipFile zipFile = new ZipFile(f); String strPath, gbkPath, strtemp; File tempFile = new File(sFileSavePath);// 从当前目录开始 strPath = tempFile.getAbsolutePath();// 输出的绝对位置 Enumeration<ZipEntry> e = zipFile.getEntries(); while (e.hasMoreElements()) { org.apache.tools.zip.ZipEntry zipEnt = e.nextElement(); gbkPath = zipEnt.getName(); if (zipEnt.isDirectory()) { strtemp = strPath + File.separator + gbkPath; File dir = new File(strtemp); dir.mkdirs(); continue; } else { // 读写文件 InputStream is = zipFile.getInputStream(zipEnt); BufferedInputStream bis = new BufferedInputStream(is); gbkPath = zipEnt.getName(); strtemp = strPath + File.separator + gbkPath; // 建目录 String strsubdir = gbkPath; for (int i = 0; i < strsubdir.length(); i++) { if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) { String temp = strPath + File.separator + strsubdir.substring(0, i); File subdir = new File(temp); if (!subdir.exists()) subdir.mkdir(); } } FileOutputStream fos = new FileOutputStream(strtemp); BufferedOutputStream bos = new BufferedOutputStream(fos); int len; byte[] buff = new byte[1024]; while ((len = bis.read(buff)) != -1) { bos.write(buff, 0, len); } bos.close(); fos.close(); } } } catch (Exception e) { throw new Exception("压缩文件解压失败,失败原因"+e.toString()); } if (isDelete) { new File(sZipFilePath).delete(); } }
2.自己新建一个main方法运行就ok啦,有问题欢迎留言讨论!
* *** *** *
* * * * * *
** ** **
*人生总是起起落落*
******************

浙公网安备 33010602011771号