[Java] java 压缩和解压缩文件 →→→→→进入此内容的聊天室

来自 , 2020-03-23, 写在 Java, 查看 146 次.
URL http://www.code666.cn/view/0ff39bbb
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Enumeration;
  7. import org.apache.tools.zip.ZipEntry;
  8. import org.apache.tools.zip.ZipFile;
  9. import org.apache.tools.zip.ZipOutputStream;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. /**
  14.  * 压缩,解压,删除,拷贝操作。
  15.  *
  16.  * 注意:此类中用到的压缩类ZipEntry等都来自于org.apache.tools包而非java.util。此包在ant.jar中有。
  17.  *
  18.  * * @author Tony
  19.  */
  20. public class FileUtil {
  21.         protected static Logger log = LoggerFactory.getLogger(FileUtil.class);
  22.  
  23.         /**
  24.          *
  25.          * 压缩文件
  26.          *
  27.          * @param inputFileName
  28.          *            要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\
  29.          *
  30.          * @param outputFileName
  31.          *            输出zip文件的路径,例如:c:\\a.zip
  32.          *
  33.          */
  34.         public static void zip(String inputFileName, String outputFileName)
  35.                         throws Exception {
  36.                 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
  37.                                 outputFileName));
  38.                 zip(out, new File(inputFileName), "");
  39.                 log.debug("压缩完成!");
  40.                 out.closeEntry();
  41.                 out.close();
  42.         }
  43.  
  44.         /**
  45.          *
  46.          * 压缩文件
  47.          *
  48.          * @param out
  49.          *            org.apache.tools.zip.ZipOutputStream
  50.          *
  51.          * @param file
  52.          *            待压缩的文件
  53.          *
  54.          * @param base
  55.          *            压缩的根目录
  56.          *
  57.          */
  58.         private static void zip(ZipOutputStream out, File file, String base)
  59.                         throws Exception {
  60.                 if (file.isDirectory()) {
  61.                         File[] fl = file.listFiles();
  62.                         base = base.length() == 0 ? "" : base + File.separator;
  63.                         for (int i = 0; i < fl.length; i++) {
  64.                                 zip(out, fl[i], base + fl[i].getName());
  65.                         }
  66.                 } else {
  67.                         out.putNextEntry(new ZipEntry(base));
  68.                         log.debug("添加压缩文件:" + base);
  69.                         FileInputStream in = new FileInputStream(file);
  70.                         int b;
  71.                         while ((b = in.read()) != -1) {
  72.                                 out.write(b);
  73.                         }
  74.                         in.close();
  75.                 }
  76.         }
  77.  
  78.         /**
  79.          *
  80.          * 解压zip文件
  81.          *
  82.          * @param zipFileName
  83.          *            待解压的zip文件路径,例如:c:\\a.zip
  84.          *
  85.          * @param outputDirectory
  86.          *            解压目标文件夹,例如:c:\\a\
  87.          *
  88.          */
  89.         public static void unZip(String zipFileName, String outputDirectory)
  90.                         throws Exception {
  91.                 ZipFile zipFile = new ZipFile(zipFileName);
  92.                 try {
  93.                         Enumeration<?> e = zipFile.getEntries();
  94.                         ZipEntry zipEntry = null;
  95.                         createDirectory(outputDirectory, "");
  96.                         while (e.hasMoreElements()) {
  97.                                 zipEntry = (ZipEntry) e.nextElement();
  98.                                 log.debug("解压:" + zipEntry.getName());
  99.                                 if (zipEntry.isDirectory()) {
  100.                                         String name = zipEntry.getName();
  101.                                         name = name.substring(0, name.length() - 1);
  102.                                         File f = new File(outputDirectory + File.separator + name);
  103.                                         f.mkdir();
  104.                                         log.debug("创建目录:" + outputDirectory + File.separator + name);
  105.                                 } else {
  106.                                         String fileName = zipEntry.getName();
  107.                                         fileName = fileName.replace('\\', '/');
  108.                                         if (fileName.indexOf("/") != -1) {
  109.                                                 createDirectory(outputDirectory, fileName.substring(0,
  110.                                                                 fileName.lastIndexOf("/")));
  111.                                                 fileName = fileName.substring(
  112.                                                                 fileName.lastIndexOf("/") + 1,
  113.                                                                 fileName.length());
  114.                                         }
  115.                                         File f = new File(outputDirectory + File.separator
  116.                                                         + zipEntry.getName());
  117.                                         f.createNewFile();
  118.                                         InputStream in = zipFile.getInputStream(zipEntry);
  119.                                         FileOutputStream out = new FileOutputStream(f);
  120.                                         byte[] by = new byte[1024];
  121.                                         int c;
  122.                                         while ((c = in.read(by)) != -1) {
  123.                                                 out.write(by, 0, c);
  124.                                         }
  125.                                         in.close();
  126.                                         out.close();
  127.                                 }
  128.                         }
  129.                 } catch (Exception ex) {
  130.                         System.out.println(ex.getMessage());
  131.                 } finally {
  132.                         zipFile.close();
  133.                         log.debug("解压完成!");
  134.                 }
  135.         }
  136.  
  137. }
  138.  

回复 "java 压缩和解压缩文件"

这儿你可以回复上面这条便签

captcha