Java实现zip解压缩目录中的所有文件详解编程语言

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

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() && !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]); 
                } 
            } 
        } 
    } 
}

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10466.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论