Java压缩技术(三) ZIP解压缩——Java原生实现

转载自   Java压缩技术(三) ZIP解压缩——Java原生实现

解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如: 

Java代码  
  1. CheckedInputStream cis = new CheckedInputStream(new FileInputStream(  
  2.         srcFile), new CRC32());  
  3.   
  4. ZipInputStream zis = new ZipInputStream(cis);  

需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示: 
Java代码  
  1. /** 
  2.  * 文件探针 
  3.  *  
  4.  *  
  5.  * 当父目录不存在时,创建目录! 
  6.  *  
  7.  *  
  8.  * @param dirFile 
  9.  */  
  10. private static void fileProber(File dirFile) {  
  11.   
  12.     File parentFile = dirFile.getParentFile();  
  13.     if (!parentFile.exists()) {  
  14.   
  15.         // 递归寻找上级目录  
  16.         fileProber(parentFile);  
  17.   
  18.         parentFile.mkdir();  
  19.     }  
  20.   
  21. }  

在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示: 
Java代码  
  1. /** 
  2.  * 文件 解压缩 
  3.  *  
  4.  * @param destFile 
  5.  *            目标文件 
  6.  * @param zis 
  7.  *            ZipInputStream 
  8.  * @throws Exception 
  9.  */  
  10. private static void decompress(File destFile, ZipInputStream zis)  
  11.         throws Exception {  
  12.   
  13.     ZipEntry entry = null;  
  14.     while ((entry = zis.getNextEntry()) != null) {  
  15.   
  16.         // 文件  
  17.         String dir = destFile.getPath() + File.separator + entry.getName();  
  18.   
  19.         File dirFile = new File(dir);  
  20.   
  21.         // 文件检查  
  22.         fileProber(dirFile);  
  23.   
  24.             if (entry.isDirectory()){  
  25.                 dirFile.mkdirs();  
  26.             } else {  
  27.             decompressFile(dirFile, zis);  
  28.             }  
  29.   
  30.             zis.closeEntry();  
  31.     }  
  32. }  

最核心的解压缩实现,其实与压缩实现非常相似,代码如下所示: 
Java代码  
  1. /** 
  2.  * 文件解压缩 
  3.  *  
  4.  * @param destFile 
  5.  *            目标文件 
  6.  * @param zis 
  7.  *            ZipInputStream 
  8.  * @throws Exception 
  9.  */  
  10. private static void decompressFile(File destFile, ZipInputStream zis)  
  11.         throws Exception {  
  12.   
  13.     BufferedOutputStream bos = new BufferedOutputStream(  
  14.             new FileOutputStream(destFile));  
  15.   
  16.     int count;  
  17.     byte data[] = new byte[BUFFER];  
  18.     while ((count = zis.read(data, 0, BUFFER)) != -1) {  
  19.         bos.write(data, 0, count);  
  20.     }  
  21.   
  22.     bos.close();  
  23. }  

来个完整的解压缩实现,代码如下: 
Java代码  
  1. /** 
  2.  * 2010-4-12 
  3.  */  
  4. package org.zlex.commons.io;  
  5.   
  6. import java.io.BufferedInputStream;  
  7. import java.io.BufferedOutputStream;  
  8. import java.io.File;  
  9. import java.io.FileInputStream;  
  10. import java.io.FileOutputStream;  
  11. import java.util.zip.CRC32;  
  12. import java.util.zip.CheckedInputStream;  
  13. import java.util.zip.CheckedOutputStream;  
  14. import java.util.zip.ZipEntry;  
  15. import java.util.zip.ZipInputStream;  
  16. import java.util.zip.ZipOutputStream;  
  17.   
  18. /** 
  19.  * ZIP压缩工具 
  20.  *  
  21.  * @author 梁栋    
  22.  * @since 1.0 
  23.  */  
  24. public class ZipUtils {  
  25.   
  26.     public static final String EXT = ".zip";  
  27.     private static final String BASE_DIR = "";  
  28.     private static final String PATH = File.separator;  
  29.     private static final int BUFFER = 1024;  
  30.   
  31.     /** 
  32.      * 文件 解压缩 
  33.      *  
  34.      * @param srcPath 
  35.      *            源文件路径 
  36.      *  
  37.      * @throws Exception 
  38.      */  
  39.     public static void decompress(String srcPath) throws Exception {  
  40.         File srcFile = new File(srcPath);  
  41.   
  42.         decompress(srcFile);  
  43.     }  
  44.   
  45.     /** 
  46.      * 解压缩 
  47.      *  
  48.      * @param srcFile 
  49.      * @throws Exception 
  50.      */  
  51.     public static void decompress(File srcFile) throws Exception {  
  52.         String basePath = srcFile.getParent();  
  53.         decompress(srcFile, basePath);  
  54.     }  
  55.   
  56.     /** 
  57.      * 解压缩 
  58.      *  
  59.      * @param srcFile 
  60.      * @param destFile 
  61.      * @throws Exception 
  62.      */  
  63.     public static void decompress(File srcFile, File destFile) throws Exception {  
  64.   
  65.         CheckedInputStream cis = new CheckedInputStream(new FileInputStream(  
  66.                 srcFile), new CRC32());  
  67.   
  68.         ZipInputStream zis = new ZipInputStream(cis);  
  69.   
  70.         decompress(destFile, zis);  
  71.   
  72.         zis.close();  
  73.   
  74.     }  
  75.   
  76.     /** 
  77.      * 解压缩 
  78.      *  
  79.      * @param srcFile 
  80.      * @param destPath 
  81.      * @throws Exception 
  82.      */  
  83.     public static void decompress(File srcFile, String destPath)  
  84.             throws Exception {  
  85.         decompress(srcFile, new File(destPath));  
  86.   
  87.     }  
  88.   
  89.     /** 
  90.      * 文件 解压缩 
  91.      *  
  92.      * @param srcPath 
  93.      *            源文件路径 
  94.      * @param destPath 
  95.      *            目标文件路径 
  96.      * @throws Exception 
  97.      */  
  98.     public static void decompress(String srcPath, String destPath)  
  99.             throws Exception {  
  100.   
  101.         File srcFile = new File(srcPath);  
  102.         decompress(srcFile, destPath);  
  103.     }  
  104.   
  105.     /** 
  106.      * 文件 解压缩 
  107.      *  
  108.      * @param destFile 
  109.      *            目标文件 
  110.      * @param zis 
  111.      *            ZipInputStream 
  112.      * @throws Exception 
  113.      */  
  114.     private static void decompress(File destFile, ZipInputStream zis)  
  115.             throws Exception {  
  116.   
  117.         ZipEntry entry = null;  
  118.         while ((entry = zis.getNextEntry()) != null) {  
  119.   
  120.             // 文件  
  121.             String dir = destFile.getPath() + File.separator + entry.getName();  
  122.   
  123.             File dirFile = new File(dir);  
  124.   
  125.             // 文件检查  
  126.             fileProber(dirFile);  
  127.   
  128.             if (entry.isDirectory()) {  
  129.                 dirFile.mkdirs();  
  130.             } else {  
  131.                 decompressFile(dirFile, zis);  
  132.             }  
  133.   
  134.             zis.closeEntry();  
  135.         }  
  136.     }  
  137.   
  138.     /** 
  139.      * 文件探针 
  140.      *  
  141.      *  
  142.      * 当父目录不存在时,创建目录! 
  143.      *  
  144.      *  
  145.      * @param dirFile 
  146.      */  
  147.     private static void fileProber(File dirFile) {  
  148.   
  149.         File parentFile = dirFile.getParentFile();  
  150.         if (!parentFile.exists()) {  
  151.   
  152.             // 递归寻找上级目录  
  153.             fileProber(parentFile);  
  154.   
  155.             parentFile.mkdir();  
  156.         }  
  157.   
  158.     }  
  159.   
  160.     /** 
  161.      * 文件解压缩 
  162.      *  
  163.      * @param destFile 
  164.      *            目标文件 
  165.      * @param zis 
  166.      *            ZipInputStream 
  167.      * @throws Exception 
  168.      */  
  169.     private static void decompressFile(File destFile, ZipInputStream zis)  
  170.             throws Exception {  
  171.   
  172.         BufferedOutputStream bos = new BufferedOutputStream(  
  173.                 new FileOutputStream(destFile));  
  174.   
  175.         int count;  
  176.         byte data[] = new byte[BUFFER];  
  177.         while ((count = zis.read(data, 0, BUFFER)) != -1) {  
  178.             bos.write(data, 0, count);  
  179.         }  
  180.   
  181.         bos.close();  
  182.     }  
  183.   
  184. }  
其实,理解了ZIP的工作原理,这些代码看起来很好懂!  
把刚才做的压缩文件再用上述代码解开看看,测试用例如下: 
Java代码  
  1. /** 
  2.  * 2010-4-12 
  3.  */  
  4. package org.zlex.commons.io;  
  5.   
  6. import static org.junit.Assert.*;  
  7.   
  8. import org.junit.Test;  
  9.   
  10. /** 
  11.  *  
  12.  * @author 梁栋 
  13.  * @version 1.0 
  14.  * @since 1.0 
  15.  */  
  16. public class ZipUtilsTest {  
  17.   
  18.     /** 
  19.      *   
  20.      */  
  21.     @Test  
  22.     public void test() throws Exception {  
  23.         // 解压到指定目录  
  24.         ZipUtils.decompress("d:\\f.txt.zip""d:\\ff");  
  25.         // 解压到当前目录  
  26.         ZipUtils.decompress("d:\\fd.zip");  
  27.     }  
  28.   
  29. }  
完整代码详见附件!
java原生的ZIP实现虽然在压缩时会因与系统字符集不符产生中文乱码,但在解压缩后,字符集即可恢复。 

除了java原生的ZIP实现外,commons和ant也提供了相应的ZIP算法实现,有机会我再一一介绍!


  • ZipUtils.rar (2.1 KB)
  • 下载次数: 526


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/327874.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

19年8月 字母哥 第二章 RESTFul接口实现与测试 看到这里了

第二章 RESTFul接口实现与测试 2.1.RESTFul接口与http协议状态表述 2.2.常用注解开发一个RESTFul接口 2.2看完了 2.3 JSON数据处理与PostMan测试 树哪里是可以加上去的 list<treeNode> listTreeNode; 为空就不显示了 20%是常用的 80%是不常用的 我只是讲解了20…

java实现验证码3秒刷新一次

<?xml version"1.0" encoding"UTF-8"?> <web-app xmlns"http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://xmlns.jcp.org/xml/ns/javaeehttp://x…

Hibernate中使用Criteria查询及注解——(Dept.hbm.xml)

Dept.hbm.xml 部门表的映射文件: <?xml version"1.0" encoding"utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&qu…

CentOS上安装SQL Server vNext CTP1

今天微软正式发布上SQL Server 2016 SP1,根据以往的SP1定律&#xff0c;可以在生产环境上使用了。打了SP1的标准版将具有企业版几乎所有的的功能。只有RAM 超过128GB或者超过24核心或者超过4路的环境才必须要安装企业版。 还有一个重要的发布&#xff1a;SQL Server vNext on L…

python弹出窗口 闪烁_Python。得到闪烁/闪烁的窗口

首先&#xff0c;大多数程序通过调用^{}(或包装它的高级函数)来刷新窗口。但是&#xff0c;有一些应用程序主要来自微软&#xff0c;他们做一些定制的东西&#xff0c;在终端用户看来就像是在闪窗&#xff0c;但在封面下看起来可能不一样。希望你不在乎任何这样的定制应用。在无…

Java压缩技术(七) TAR——Commons实现

转载自 Java压缩技术&#xff08;七&#xff09; TAR——Commons实现 顺便复习一遍linux命令&#xff1a; tar cf <file.tar> <file>将由文件<file>创建名为<file.tar>归档文件&#xff0c;同时保留原文件。 tar xf <file.tar>将由归档文件<…

[干货来袭]MSSQL Server on Linux预览版安装教程(先帮大家踩坑)

前言 昨天晚上微软爸爸开了全国开发者大会,会上的内容,我就不多说了,园子里面很多.. 我们唐总裁在今年曾今透漏过SQL Server love Linux,果不其然,这次开发者大会上就推出了MSSQL Server on Linux预览版 官方地址:https://docs.microsoft.com/zh-cn/sql/linux/ E文好的可以自己…

MVC三层架构理解

MVC三层架构 什么是MVC&#xff1a; Model view Controller 模型、视图、控制器 以前的架构 用户直接访问控制层&#xff0c;控制层就可以直接操作数据库&#xff1b; servlet--CRUD-->数据库 弊端&#xff1a;程序十分臃肿&#xff0c;不利于维护 servlet的代码中&…

spring boot建立项目 git推送giteee

gitee上创建项目时候不要创建readme.md 创建完全空的项目 不然上次会报错的 $ git init 初始化git $ git status $ git add . $ git status $ git commit -am 初次建立项目 $ git remote add origin https://gitee.com/yjb1091947832/yangjiabin.git…

vba mysql update多字段_vba操作Mysql使用UPDATE一次更新多组数据

网上查到综合后确定的update语法范例&#xff1a;UPDATE mytable SET myfield CASE WHEN 1 THEN ‘value‘ WHEN 2 THEN ‘value‘ WHEN 3 THEN ‘value‘ END WHERE id IN (1,2,3)下面是我写的一个通用的update库表内容过程&#xff1a;kku为库表&#xff0c;zd为字段&#x…

Hibernate中使用Criteria查询及注解——(hibernate.cfg.xml)

hibernate.cfg.xml hibernate主配置文件&#xff1a; <?xml version1.0 encodingUTF-8?> <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration…

为什么我不选阿里云(一)

我是资深阿里黑&#xff0c;“资深”体现在我黑阿里和阿里云从来有理有据&#xff0c;我不是小白用户&#xff0c;我本身就是云架构师&#xff0c;我目前主要推荐中国客户上Azure和AWS。 阿里巴巴&#xff08;BABA&#xff09;是一家怎样的公司 阿里巴巴在中国看上去很高大上&a…

Java压缩技术(六) BZIP2——Commons实现

转载自 Java压缩技术&#xff08;六&#xff09; BZIP2——Commons实现 BZip2与GZip有什么渊源&#xff0c;我这里不深究。我要说的只是&#xff0c;这两种算法&#xff0c;你在linux下都可以找到相应的操作命令。GZip 压缩 gzip <file> 将得到压缩文件<file>.gz&…

微信支付师兄

https://www.jianshu.com/writer#/notebooks/41472123/notes/57967685

vpn mysql_MYSQL数据库

1.关系型数据库相关概念关系Relational &#xff1a;关系就是二维表&#xff0c;其中&#xff1a;表中的行、列次序并不重要行row&#xff1a;表中的每一行&#xff0c;又称为一条记录record列column&#xff1a;表中的每一列&#xff0c;称为属性&#xff0c;字段&#xff0c;…

Stateless 3.0——.NET Core上的状态机库

Stateless是一个基于C#&#xff0c;创建状态机的简单库&#xff0c;最新版本支持.NET Core 1.0。其实现方式并不是通过.NET Core&#xff0c;而是通过写入.NET Standard实现的。就像Android平台上API级别抽象出许多底层版本的Android&#xff0c;.NET Standard是一组所有.NET平…

Java压缩技术(五) GZIP相关——浏览器解析

转载自 Java压缩技术&#xff08;五&#xff09; GZIP相关——浏览器解析 GZIP本身就是一种网络流压缩算法&#xff0c;而且应用相当广泛。如果网络访问过程中&#xff0c;其数据流较大&#xff0c;势必降低网络访问效率&#xff0c;此时就需要考虑使用压缩&#xff01;当然&…

在实际使用中 mysql所支持的触发器有_2016计算机二级MySQL冲刺题及答案

2016计算机二级MySQL冲刺题及答案11[简答题] 请使用UPDATE语句将数据库db_test的表content中留言人姓名为“MySQL初学者”的留言内容修改为“如何使用INSERT语句?”。参考解析&#xff1a;在MySQL命令行客户端输入如下SQL语句即可实现&#xff1a;mysql>USE db-test;Databa…

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c 解决MySQL报错&#xff1a;1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column informat [Err] 1055 - Expression #1 of ORDER BY …

在mysql中插入日期

preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));//给第五个占位符&#xff1f; 的值赋值为new Date(new java.util.Date().getTime())&#xff1b; 外面的Date是SQL中的java.sql.*&#xff1b; 里面的Date是java中的java.util.Date&#xff1b;