java 处理zip压缩/解压 文件/目录 您所在的位置:网站首页 java压缩文件夹多一层目录 java 处理zip压缩/解压 文件/目录

java 处理zip压缩/解压 文件/目录

2023-12-11 12:06| 来源: 网络整理| 查看: 265

环境

操作系统: win7 java: jdk7

需求

需要把多个jar进行打包,方便上传。

步骤

这里我们需要用到的java api有:ZipOutputStream、ZipEntry。

单个文件的压缩 public static void main(String[] args) { //C:\\Users\\yutao\\Desktop\\pageage String zipFileName = "C:\\Users\\yutao\\Desktop\\pageage\\ziptest.zip"; String entry = "C:\\Users\\yutao\\Desktop\\pageage\\ggjob-searchfull.jar"; compress(zipFileName, entry); } /** * 压缩文件 * @param zipFileName * @param entry * @author yutao * @date 2017年5月24日下午2:21:11 */ private static void compress(String zipFileName, String entry) { try { ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName))); //设置压缩级别 zos.setLevel(Deflater.BEST_COMPRESSION); File entryFile = new File(entry); // entryFile.listFiles(); if(!entryFile.exists()){ System.out.println("The entry file " + entryFile.getAbsolutePath()); zos.close(); return; } ZipEntry ze = new ZipEntry(entryFile.getName()); System.out.println(ze.getName());//条码名称 System.out.println(ze.getComment()); System.out.println(ze.getCompressedSize()); System.out.println(ze.getSize()); System.out.println(ze.getMethod()); System.out.println(ze.getTime()); System.out.println(ze.getCrc()); System.out.println(ze.getExtra()); zos.putNextEntry(ze); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFile)); byte[] buffer = new byte[1024]; int count = -1; bis.read(); while((count = bis.read(buffer)) != -1){ zos.write(buffer, 0, count); } bis.close(); zos.closeEntry(); zos.close(); } catch (IOException e) { e.printStackTrace(); } } 多文件压缩

这里写图片描述

下面我们要对上面的情况进行压缩,代码如下:

public static void main(String[] args) throws Exception { String entry = "C:\\Users\\yutao\\Desktop\\pageage\\test";//需要压缩的文件目录 File file = new File(entry); ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file.getAbsolutePath() + ".zip"))); String base = file.getName(); compressZip(zipOutput, file, base); zipOutput.closeEntry(); zipOutput.close(); } /** * 因为子文件夹中可能还有文件夹,所以进行递归 * */ private static void compressZip(ZipOutputStream zipOutput, File file, String base) throws IOException { if(file.isDirectory()){ File[] listFiles = file.listFiles();// 列出所有的文件 for(File fi : listFiles){ if(fi.isDirectory()){ compressZip(zipOutput, fi, base + "/" + fi.getName()); }else{ zip(zipOutput, fi, base); } } }else{ zip(zipOutput, file, base); } } /** * 压缩的具体操作 * */ public static void zip(ZipOutputStream zipOutput, File file, String base) throws IOException, FileNotFoundException { ZipEntry zEntry = new ZipEntry(base + File.separator + file.getName()); zipOutput.putNextEntry(zEntry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int read = 0; while((read =bis.read(buffer)) != -1){ zipOutput.write(buffer, 0, read); } bis.close(); }

重点:

①ZipEntry其实就是即将要压缩文件的文件信息。 假设我们即将要压缩的是ggindex.jar文件,路径是根路径。 那么我们就要在ZipEntry对象中设置好。

ZipEntry zEntry = new ZipEntry(base + File.separator + file.getName()); zipOutput.putNextEntry(zEntry); //根据上述的假设,我们就要这样写 ZipEntry zEntry = new ZipEntry("ggindex.jar"); zipOutput.putNextEntry(zEntry);//将其添加到压缩包中,准备进行压缩啦。

这里还要注意,ZipEntry参数base + File.separator + file.getName()是为了产生下面这样的效果。否则所有的文件都将会在根路径中。

这里写图片描述

解压

解压缩的关键就是通过ZipInputStream对象的getNextEntry()方法拿到ZipEntry。这个是压缩包里的压缩文件(条目)。

public static void main(String[] args) throws Exception { String entryFile = "C:\\Users\\yutao\\Desktop\\pageage\\test.zip"; File file = new File(entryFile); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file))); File fout = null; String parent = file.getParent(); ZipEntry entry; while((entry = zis.getNextEntry())!=null && !entry.isDirectory()){ fout = new File(parent, entry.getName()); if(!fout.exists()){ (new File(fout.getParent())).mkdirs(); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout)); int b = -1; byte[] buffer = new byte[1024]; while((b = zis.read(buffer))!= -1){ bos.write(buffer, 0, b); } bos.close(); } zis.close(); } 总结

知道java的zip压缩原理后,其实还是很简单的!

①压缩时一定要用到ZipOutputStream和ZipEntry,解压时,一定要用到ZipInputStream和ZipEntry。 ②压缩时,既包含文件又包含目录时,基本上就要使用递归啦,路径一定要记得设置好。 ③由于ZIP对每个文件进行单独压缩而没有利用文件间的冗余信息(即固实压缩),所以ZIP的压缩率会稍逊于tar压缩包。

参考地址: http://www.imooc.com/wenda/detail/254317

http://www.cnblogs.com/lbangel/p/3459092.html

http://www.yiibai.com/java_io/java_io_zip_file.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有