Java实现Zip压缩目录中的所有文件
发布时间:2020-12-14 23:24:29 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.*;import java.util.*;import java.util.zip.*;public class FolderZip {private static String sourcepath="C:temp";private stati
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import java.io.*; import java.util.*; import java.util.zip.*; public class FolderZip { private static String sourcepath="C:temp"; private static List<String>folderList=new ArrayList<String>(Arrays.asList(sourcepath)); private static List<String>folderList2=new ArrayList<String>(Arrays.asList("D:tmp"+File.separator+sourcepath.substring(sourcepath.lastIndexOf(File.separator)))); private static FileInputStream fis = null; private static FileOutputStream fos = null; private static ZipOutputStream zipOut = 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 zipFile = new File(folderList2.get(j),temp.getName() + ".zip"); if (temp.isFile() && !zipFile.exists()) try { fis = new FileInputStream(temp); fos = new FileOutputStream(zipFile); zipOut = new ZipOutputStream(fos); ZipEntry entry = new ZipEntry(temp.getName()); zipOut.putNextEntry(entry); int nNumber; byte[] buffer = new byte[20480]; while ((nNumber = fis.read(buffer)) != -1) zipOut.write(buffer,nNumber); zipOut.flush(); } catch (IOException e) { continue; } finally { try { zipOut.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站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |