Java实现Zip压缩/解压缩目录中的所有文件

Java实现zip解压缩目录中的所有文件

import java.io.*;  

02 import java.util.*;  

03 import java.util.zip.*;  

04 public class FolderUnzip {  

05 private static String sourcepath="D:\\tmp";  

06 private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));  

07 private static List<String>folderList2=new ArrayList<String>(Arrays.asList("E:\\tt"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));  

08 private static FileInputStream fis = null;  

09 private static FileOutputStream fos = null;  

10 private static ZipInputStream zipin = 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 originalFile = null;  

22                 if (temp.getName().endsWith(".zip"))  

23                     originalFile = new File(folderList2.get(j), temp.getName()  

24                             .substring(0, temp.getName().lastIndexOf('.')));  

25                 if (temp.isFile() && !originalFile.exists()) {  

26                     try {  

27                         fis = new FileInputStream(temp);  

28                         zipin = new ZipInputStream(fis);  

29             ZipEntry entry = zipin.getNextEntry();  

30             fos = new FileOutputStream(new File(folderList2.get(j),entry.getName()));  

31                         byte[] buffer = new byte[20480];  

32                         int nNumber;  

33                         while ((nNumber = zipin.read(buffer, 0, buffer.length)) != -1)  

34                             fos.write(buffer, 0, nNumber);  

35                         fos.flush();  

36                     } catch (IOException e) {  

37                         continue;  

38                     } finally {  

39                         try {  

40                             zipin.close();  

41                             fos.close();  

42                             fis.close();  

43                         } catch (IOException e) {  

44                         }  

45                     }  

46                 } else if (temp.isDirectory()) {  

47                     folderList.add(folderList.get(j) + File.separator + file[i]);  

48                     folderList2.add(folderList2.get(j) + File.separator+ file[i]);  

49                 }  

50             }  

51         }  

52     }  

53 } 


 

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

import java.io.*;  

02 import java.util.*;  

03 import java.util.zip.*;  

04 public class FolderZip {  

05 private static String sourcepath="C:\\temp";  

06 private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));  

07 private static List<String>folderList2=new ArrayList<String>(Arrays.asList("D:\\tmp"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));  

08 private static FileInputStream fis = null;  

09 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 } 


 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值