首页 > Java > java教程 > 正文

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

大家讲道理
发布: 2016-11-10 10:44:55
原创
1480 人浏览过

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(&#39;.&#39;)));
                if (temp.isFile() && !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]);
                }
            }
        }
    }
}
登录后复制

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板