返回顶部

收藏

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

更多
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class FolderUnzip {
private static String sourcepath="D:\\tmp";
private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath));
private static List<String>folderList2=new ArrayList<String>(Arrays.asList("E:\\tt"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator))));
private static FileInputStream fis = null;
private static FileOutputStream fos = null;
private static ZipInputStream zipin = null;
    public static void main(String[] args) {
        for (int j = 0; j < folderList.size(); j++) {
            new File(folderList2.get(j)).mkdirs();
            String[] file = new File(folderList.get(j)).list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (folderList.get(j).endsWith(File.separator))
                    temp = new File(folderList.get(j), file[i]);
                else
                    temp = new File(folderList.get(j), file[i]);
                File originalFile = null;
                if (temp.getName().endsWith(".zip"))
                    originalFile = new File(folderList2.get(j), temp.getName()
                            .substring(0, temp.getName().lastIndexOf('.')));
                if (temp.isFile() &amp;&amp; !originalFile.exists()) {
                    try {
                        fis = new FileInputStream(temp);
                        zipin = new ZipInputStream(fis);
            ZipEntry entry = zipin.getNextEntry();
            fos = new FileOutputStream(new File(folderList2.get(j),entry.getName()));
                        byte[] buffer = new byte[20480];
                        int nNumber;
                        while ((nNumber = zipin.read(buffer, 0, buffer.length)) != -1)
                            fos.write(buffer, 0, nNumber);
                        fos.flush();
                    } catch (IOException e) {
                        continue;
                    } finally {
                        try {
                            zipin.close();
                            fos.close();
                            fis.close();
                        } catch (IOException e) {
                        }
                    }
                } else if (temp.isDirectory()) {
                    folderList.add(folderList.get(j) + File.separator + file[i]);
                    folderList2.add(folderList2.get(j) + File.separator+ file[i]);
                }
            }
        }
    }
}

标签:java

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2014-06-24 00:00:00 Java Microbenchmarking Harness(jmh)
  2. 博主 发表 2014-06-23 00:00:00 Java Object Layout(jol)
  3. 博主 发表 2014-06-23 00:00:00 Java Concurrency Stress(jcstress)
  4. 博主 发表 2014-05-17 00:00:00 Java Performance:The Definitive Guide Chapter 4
  5. 博主 发表 2014-05-15 00:00:00 Java Performance:The Definitive Guide Chapter 3
  6. 博主 发表 2014-04-27 00:00:00 Java Performance:The Definitive Guide Chapter 1 2
  7. 博主 发表 2014-04-22 00:00:00 java core dump analysis
  8. 博主 发表 2014-04-16 00:00:00 java cpu monitor
  9. 博主 发表 2014-03-11 00:00:00 Java Mission Control
  10. TiuVe2 发表 2017-10-01 12:18:44 Java 技术之垃圾回收机制
  11. TiuVe2 发表 2017-10-01 12:27:10 Java 守护线程概述
  12. TiuVe2 发表 2017-10-06 15:29:51 Java 里如何实现线程间通信

发表评论