[Java] java压缩指定目录下的所有文件和文件夹 →→→→→进入此内容的聊天室

来自 , 2019-09-28, 写在 Java, 查看 98 次.
URL http://www.code666.cn/view/7fc63ff0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.zip.*;
  4. String sourceDir="E:\\test";
  5. int parentDirectoryLen=sourceDir.lastIndexOf(File.separator)+1;
  6. File[] copyfoldersList = new File(sourceDir).listFiles();
  7. FileOutputStream fos = new FileOutputStream("E:\\test.zip");
  8. ZipOutputStream zipOut = new ZipOutputStream(fos);
  9. for (int k = 0; k < copyfoldersList.length; k++) {
  10.         if (copyfoldersList[k].isDirectory()) {
  11.                 LinkedList<String> copysourcepath = new LinkedList<String>(Arrays.asList(copyfoldersList[k].getAbsolutePath()));
  12.                 while (copysourcepath.size() > 0) {
  13.                         File folders = new File(copysourcepath.peek());
  14.                         String[] file = folders.list();
  15.                         for (int i = 0; i < file.length; i++) {
  16.                                 File ff = new File(copysourcepath.peek(), file[i]);
  17.                                 if (ff.isFile()) {
  18.                                         FileInputStream fis =null;
  19.                                         try {
  20.                                                 fis = new FileInputStream(ff);
  21.                                                 ZipEntry entry = new ZipEntry(ff.getAbsoluteFile().substring(parentDirectoryLen));
  22.                                                 zipOut.putNextEntry(entry);
  23.                                                 int nNumber;
  24.                                                 byte[] buffer = new byte[Long.MIN_VALUE];
  25.                                                 while ((nNumber = fis.read(buffer)) != -1)
  26.                                                         zipOut.write(buffer, 0, nNumber);
  27.                                         } catch (IOException e) {
  28.                                                 e.printStackTrace();
  29.                                                 zipOut.close();
  30.                                                 fos.close();
  31.                                         }finally{
  32.                                                 try {
  33.                                                         fis.close();
  34.                                                 } catch (IOException e) {}
  35.                                         }
  36.                                 } else if (ff.isDirectory()) {
  37.                                         for (File f : temp.listFiles()) {
  38.                                                 if (f.isDirectory())
  39.                                                         copysourcepath.add(f.getPath());
  40.                                                 else if (f.isFile()) {
  41.                                                         FileInputStream fis =null;
  42.                                                         try {
  43.                                                                 fis = new FileInputStream(f);
  44.                                                                 ZipEntry entry = new ZipEntry(f.getAbsoluteFile().substring(parentDirectoryLen));
  45.                                                                 zipOut.putNextEntry(entry);
  46.                                                                 int nNumber;
  47.                                                                 byte[] buffer = new byte[Long.MIN_VALUE];
  48.                                                                 while ((nNumber = fis.read(buffer)) != -1)
  49.                                                                         zipOut.write(buffer, 0, nNumber);
  50.                                                         } catch (IOException e) {
  51.                                                                 e.printStackTrace();
  52.                                                                 zipOut.close();
  53.                                                                 fos.close();
  54.                                                         }finally{
  55.                                                                 try {
  56.                                                                         fis.close();
  57.                                                                 } catch (IOException e) {}
  58.                                                         }
  59.                                                 }
  60.                                         }
  61.                                 }
  62.                         }
  63.                         copysourcepath.removeFirst();
  64.                 }
  65.         }
  66. }
  67. try {
  68.         zipOut.flush();
  69. } catch (IOException e) {
  70.         e.printStackTrace();
  71. }finally{
  72.         try {
  73.                 zipOut.close();
  74.                 fos.close();
  75.         } catch (IOException e) {}
  76. }
  77. //java/5714

回复 "java压缩指定目录下的所有文件和文件夹"

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

captcha