[Java] Java实现Zip压缩目录中的所有文件 →→→→→进入此内容的聊天室

来自 , 2019-04-24, 写在 Java, 查看 101 次.
URL http://www.code666.cn/view/f6876a9f
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.zip.*;
  4. public class FolderZip {
  5. private static String sourcepath="C:\\temp";
  6. private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));
  7. private static List<String>folderList2=new ArrayList<String>(Arrays.asList("D:\\tmp"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));
  8. private static FileInputStream fis = null;
  9. private static FileOutputStream fos = null;
  10. private static ZipOutputStream zipOut = null;
  11.         public static void main(String[] args) {
  12.                 for (int j = 0; j < folderList.size(); j++) {
  13.                         new File(folderList2.get(j)).mkdirs();
  14.                         String[] file = new File(folderList.get(j)).list();
  15.                         File temp = null;
  16.                         for (int i = 0; i < file.length; i++) {
  17.                                 if (folderList.get(j).endsWith(File.separator))
  18.                                         temp = new File(folderList.get(j), file[i]);
  19.                                 else
  20.                                         temp = new File(folderList.get(j), file[i]);
  21.                                 File zipFile = new File(folderList2.get(j), temp.getName()
  22.                                                 + ".zip");
  23.                                 if (temp.isFile() && !zipFile.exists())
  24.                                         try {
  25.                                                 fis = new FileInputStream(temp);
  26.                                                 fos = new FileOutputStream(zipFile);
  27.                                                 zipOut = new ZipOutputStream(fos);
  28.                                                 ZipEntry entry = new ZipEntry(temp.getName());
  29.                                                 zipOut.putNextEntry(entry);
  30.                                                 int nNumber;
  31.                                                 byte[] buffer = new byte[20480];
  32.                                                 while ((nNumber = fis.read(buffer)) != -1)
  33.                                                         zipOut.write(buffer, 0, nNumber);
  34.                                                 zipOut.flush();
  35.                                         } catch (IOException e) {
  36.                                                 continue;
  37.                                         } finally {
  38.                                                 try {
  39.                                                         zipOut.close();
  40.                                                         fos.close();
  41.                                                         fis.close();
  42.                                                 } catch (IOException e) {
  43.                                                 }
  44.                                         }
  45.                                 else if (temp.isDirectory()) {
  46.                                         folderList
  47.                                                         .add(folderList.get(j) + File.separator + file[i]);
  48.                                         folderList2.add(folderList2.get(j) + File.separator
  49.                                                         + file[i]);
  50.                                 }
  51.                         }
  52.                 }
  53.         }
  54. }
  55. //java/1345

回复 "Java实现Zip压缩目录中的所有文件"

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

captcha