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

来自 , 2020-08-06, 写在 Java, 查看 118 次.
URL http://www.code666.cn/view/da0d1111
  1. package net.szh.zip;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.util.zip.CRC32;
  8. import java.util.zip.CheckedOutputStream;
  9.  
  10. import org.apache.tools.zip.ZipEntry;
  11. import org.apache.tools.zip.ZipOutputStream;
  12.  
  13. public class ZipCompressor {
  14.         static final int BUFFER = 8192;
  15.  
  16.         private File zipFile;
  17.  
  18.         public ZipCompressor(String pathName) {
  19.                 zipFile = new File(pathName);
  20.         }
  21.  
  22.         public void compress(String srcPathName) {
  23.                 File file = new File(srcPathName);
  24.                 if (!file.exists())
  25.                         throw new RuntimeException(srcPathName + "不存在!");
  26.                 try {
  27.                         FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
  28.                         CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
  29.                                         new CRC32());
  30.                         ZipOutputStream out = new ZipOutputStream(cos);
  31.                         String basedir = "";
  32.                         compress(file, out, basedir);
  33.                         out.close();
  34.                 } catch (Exception e) {
  35.                         throw new RuntimeException(e);
  36.                 }
  37.         }
  38.  
  39.         private void compress(File file, ZipOutputStream out, String basedir) {
  40.                 /* 判断是目录还是文件 */
  41.                 if (file.isDirectory()) {
  42.                         System.out.println("压缩:" + basedir + file.getName());
  43.                         this.compressDirectory(file, out, basedir);
  44.                 } else {
  45.                         System.out.println("压缩:" + basedir + file.getName());
  46.                         this.compressFile(file, out, basedir);
  47.                 }
  48.         }
  49.  
  50.         /** 压缩一个目录 */
  51.         private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
  52.                 if (!dir.exists())
  53.                         return;
  54.  
  55.                 File[] files = dir.listFiles();
  56.                 for (int i = 0; i < files.length; i++) {
  57.                         /* 递归 */
  58.                         compress(files[i], out, basedir + dir.getName() + "/");
  59.                 }
  60.         }
  61.  
  62.         /** 压缩一个文件 */
  63.         private void compressFile(File file, ZipOutputStream out, String basedir) {
  64.                 if (!file.exists()) {
  65.                         return;
  66.                 }
  67.                 try {
  68.                         BufferedInputStream bis = new BufferedInputStream(
  69.                                         new FileInputStream(file));
  70.                         ZipEntry entry = new ZipEntry(basedir + file.getName());
  71.                         out.putNextEntry(entry);
  72.                         int count;
  73.                         byte data[] = new byte[BUFFER];
  74.                         while ((count = bis.read(data, 0, BUFFER)) != -1) {
  75.                                 out.write(data, 0, count);
  76.                         }
  77.                         bis.close();
  78.                 } catch (Exception e) {
  79.                         throw new RuntimeException(e);
  80.                 }
  81.         }
  82. }
  83.  
  84.  
  85. package net.szh.zip;
  86.  
  87. import java.io.File;
  88.  
  89. import org.apache.tools.ant.Project;
  90. import org.apache.tools.ant.taskdefs.Zip;
  91. import org.apache.tools.ant.types.FileSet;
  92.  
  93. public class ZipCompressorByAnt {
  94.  
  95.         private File zipFile;
  96.  
  97.         public ZipCompressorByAnt(String pathName) {
  98.                 zipFile = new File(pathName);
  99.         }
  100.        
  101.         public void compress(String srcPathName) {
  102.                 File srcdir = new File(srcPathName);
  103.                 if (!srcdir.exists())
  104.                         throw new RuntimeException(srcPathName + "不存在!");
  105.                
  106.                 Project prj = new Project();
  107.                 Zip zip = new Zip();
  108.                 zip.setProject(prj);
  109.                 zip.setDestFile(zipFile);
  110.                 FileSet fileSet = new FileSet();
  111.                 fileSet.setProject(prj);
  112.                 fileSet.setDir(srcdir);
  113.                 //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");
  114.                 //fileSet.setExcludes(...); 排除哪些文件或文件夹
  115.                 zip.addFileset(fileSet);
  116.                
  117.                 zip.execute();
  118.         }
  119. }
  120.  
  121.  
  122.  

回复 "java 压缩文件"

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

captcha