Java对zip格式压缩和解压缩

通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压。

1.1 ZIP和GZIP的区别

gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格式),它的设计目标是处理单个的文件。gzip在压缩文件中的数据时使用的就是zlib。为了保存与文件属性有关的信息,gzip需要在压缩文件(*.gz)中保存更多的头信息内容,而zlib不用考虑这一点。但gzip只适用于单个文件,所以我们在UNIX/Linux上经常看到的压缩包后缀都是*.tar.gz或*.tgz,也就是先用tar把多个文件打包成单个文件,再用gzip压缩的结果。
     zip只是一种数据结构,跟rar同类型。zip是适用于压缩多个文件的格式(相应的工具有PkZip和WinZip等),因此,zip文件还要进一步包含文件目录结构的信息,比gzip的头信息更多。但需要注意,zip格式可采用多种压缩算法,我们常见的zip文件大多不是用zlib的算法压缩的,其压缩数据的格式与gzip大不一样。

1.2相关类与接口:

Checksum 接口:表示数据校验和的接口,被类Adler32和CRC32实现
Adler32 :使用Alder32算法来计算Checksum数目
CRC32 :使用CRC32算法来计算Checksum数目

CheckedInputStream :InputStream派生类,可得到输入流的校验和Checksum,用于校验数据的完整性
CheckedOutputStream :OutputStream派生类,可得到输出流的校验Checksum, 用于校验数据的完整性

DeflaterOutputStream :压缩类的基类。
ZipOutputStream :DeflaterOutputStream的一个子类,把数据压缩成Zip文件格式
GZIPOutputStream :DeflaterOutputStream的一个子类,把数据压缩成GZip文件格式

InflaterInputStream :解压缩类的基类
ZipInputStream :InflaterInputStream的一个子类,能解压缩Zip格式的数据
GZIPInputStream :InflaterInputStream的一个子类,能解压缩Zip格式的数据

ZipEntry 类:表示 ZIP 文件条目
ZipFile 类:此类用于从 ZIP 文件读取条目

1.3 压缩文件

下面实例我们使用了apache的zip工具包(所在包为ant.jar ),因为java类型自带的不支持中文路径,不过两者使用的方式是一样的,只是apache压缩工具多了设置编码方式的接口,其他基本上是一样的。另外,如果使用org.apache.tools.zip.ZipOutputStream来压缩的话,我们只能使用org.apache.tools.zip.ZipEntry来解压,而不能使用java.util.zip.ZipInputStream来解压读取了,当然apache并未提供ZipInputStream类。

public
static
void compress(String srcFilePath, String destFilePath) {

File src = new File(srcFilePath);

if (!src.exists()) {

throw
new RuntimeException(srcFilePath + "不存在");

}

File zipFile = new File(destFilePath);

try {

FileOutputStream fos = new FileOutputStream(zipFile);

            CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());

            ZipOutputStream zos = new ZipOutputStream(cos);

            String baseDir = "";

            compressbyType(src, zos, baseDir);

zos.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private
static
void compressbyType(File src, ZipOutputStream zos,

String baseDir) {

if (!src.exists())

return;

System.out.println("压缩" + baseDir + src.getName());

if (src.isFile()) {

compressFile(src, zos, baseDir);

} else
if (src.isDirectory()) {

compressDir(src, zos, baseDir);

}

}

/**

* 压缩文件

*

*/

private
static
void compressFile(File file, ZipOutputStream zos,

String baseDir) {

if (!file.exists())

return;

try {

BufferedInputStream bis = new BufferedInputStream(

                    new FileInputStream(file));

            ZipEntry entry = new ZipEntry(baseDir + file.getName());

            zos.putNextEntry(entry);

            int count;

            byte[] buf = new byte[BUFSIZE];

            while ((count = bis.read(buf)) != -1) {

                zos.write(buf, 0, count);

            }

bis.close();

} catch (Exception e) {

// TODO: handle exception

}

}

/**

* 压缩文件夹

*

*/

private
static
void compressDir(File dir, ZipOutputStream zos,

String baseDir) {

if (!dir.exists())

return;

File[] files = dir.listFiles();

if(files.length == 0){

            try {

                zos.putNextEntry(new ZipEntry(baseDir + dir.getName()

                        + File.separator));

            } catch (IOException e) {

                e.printStackTrace();

            }

            

        }

for (File file : files) {

compressbyType(file, zos, baseDir + dir.getName() + File.separator);

}

}

总结步骤:

  1. 创建压缩到的文件File zipFile = new File(destFilePath);
  2. 根据zipFile生成ZipOutputStream用于写入即将被压缩的文件

    FileOutputStream fos = new FileOutputStream(zipFile);

    CheckedOutputStream cos = new                                      CheckedOutputStream(fos, new CRC32());

    ZipOutputStream zos = new ZipOutputStream(cos);

  3. 循环遍历源文件,首先需要创建ZipEntry用于标记压缩文件中含有的条目

    ZipEntry entry = new ZipEntry(baseDir + file.getName());

    然后将条目增加到ZipOutputStream中,

    zos.putNextEntry(entry);

    最后再调用要写入条目对应文件的输入流读取文件内容写入到压缩文件中。

    BufferedInputStream bis = new BufferedInputStream(

            new FileInputStream(file));

            ZipEntry entry = new ZipEntry(baseDir + file.getName());

            zos.putNextEntry(entry);

            int count;

            byte[] buf = new byte[BUFSIZE];

            while ((count = bis.read(buf)) != -1) {

                zos.write(buf, 0, count);

            }

    注意:如果是空目录直接zos.putNextEntry(new ZipEntry(baseDir +     dir.getName()+ File.separator))并不用写入文件内容,其中最主要的涉及到目录的压缩的,就是这一句话  out.putNextEntry(new ZipEntry(base + "/")); //放入一级目录 (防止空目录被丢弃)

    1.4 解压zip文件

    public
    static
    void decompress(String srcPath, String dest) throws Exception {

    File file = new File(srcPath);

    if (!file.exists()) {

    throw
    new RuntimeException(srcPath + "所指文件不存在");

    }

    ZipFile zf = new ZipFile(file);

    Enumeration
    entries = zf.getEntries();

    ZipEntry
    entry = null;

    while (entries.hasMoreElements()) {

    entry = (ZipEntry) entries.nextElement();

    System.out.println("解压" + entry.getName());

    if (entry.isDirectory()) {

    String dirPath = dest + File.separator + entry.getName();

    File dir = new File(dirPath);

    dir.mkdirs();

    } else {

    // 表示文件

    File f = new File(dest + File.separator + entry.getName());

    if (!f.exists()) {

    String dirs = FileUtils.getParentPath(f);

    File parentDir = new File(dirs);

    parentDir.mkdirs();

    }

    f.createNewFile();

    // 将压缩文件内容写入到这个文件中

    InputStream is = zf.getInputStream(entry);

    FileOutputStream fos = new FileOutputStream(f);

    int
    count;

    byte[] buf = new
    byte[8192];

    while ((count = is.read(buf)) != -1) {

    fos.write(buf, 0, count);

    }

    is.close();

    fos.close();

    }

    }

    }

Java对zip格式压缩和解压缩的更多相关文章

  1. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  2. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  3. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  4. java采用zip方式实现String的压缩和解压缩CompressStringUtil类

    CompressStringUtil类:不多说,直接贴代码: /** * 压缩 * * @param paramString * @return */ public static final byte ...

  5. Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...

  6. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  7. Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)

    1.压缩和解压缩命令    常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令        zip 压缩文件名 源文件:压缩文件   ...

  8. IO操作之使用zip包压缩和解压缩文件

    转自:http://www.cdtarena.com/java.html​​Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...

  9. java工具类——java将一串数据按照gzip方式压缩和解压缩

    我要整理在工作中用到的工具类分享出来,也方便自己以后查阅使用,这些工具类都是我自己实际工作中使用的 import java.io.ByteArrayInputStream; import java.i ...

随机推荐

  1. 【译】ASP.NET MVC 5 官方教程 - 目录

    ASP.NET MVC 5 官方教程 - 目录 [译]ASP.NET MVC 5 教程 - 1:入门 [译]ASP.NET MVC 5 教程 - 2:添加控制器 [译]ASP.NET MVC 5 教程 ...

  2. 在SharePoint列表中使用动态筛选条件[今日][Today]

    如果在SharePoint使用了日历控件或者其他列表中有时间字段,用户经常希望能够动态使用条件字段进行筛选,例如希望筛选出开始日期是今天的事件.未来三日的事件. SharePoint的列表筛选条件支持 ...

  3. 初步理解IOC和DI和AOP模式

    初步理解IOC和DI和AOP模式 控制反转(IOC) 控制反转(IOC,Inversion of Control)是一种转主动为被动关系的一种编程模式,有点类似于工厂模式,举个栗子, 下面这个这不是I ...

  4. python+pcap+dpkt抓包小实例

    通过pcap与dpkt抓包解包示例: #!/usr/bin/env python # -*- coding: utf-8 -*- """ 网络数据包捕获与分析程序 &qu ...

  5. CentOS6.3上部署Ceph

    一.背景知识 搭建ceph的机器分为两种:client和非client(mds.monitor.osd). 配置时client只需要在内核编译时选上ceph就行,而其它三种则还需要编译ceph用户态源 ...

  6. Linux—virtualbox系统安装(1)

    安装过程 1 点击新建 2 内存大小一般512M即可 3 按照默认的硬盘空间大小8G 4 选择第一个VDI 5 选择固定大小,系统运行速度快,效率高 6 保存文件位置 7 创建成功后,点击设置,将软驱 ...

  7. Visual Studio code安装步骤

    1.官方下载:https://code.visualstudio.com/,本人电脑是window系统  下载之后,双击安装,安装完之后左侧栏那边是英文,如何变为中文: 按快捷键ctrl+shift+ ...

  8. jquery分页插件pagination

    参考1:https://www.cnblogs.com/jingping/p/3925976.html 参考2:https://segmentfault.com/a/1190000014487357 ...

  9. JMeter组件之BeanShell PostProcessor的使用

    1. 场景一:获取请求响应中的数据,并保存 import com.alibaba.fastjson.*;  // 引入包.这个包需要先放在:<安装目录>\apache-jmeter-3.2 ...

  10. 「工具」三分钟了解一款在线流程绘制工具:Whimsical

    Whimsical 是一款在线流程绘制工具,只需要一个浏览器就随时随地绘制精美的流程图.除了流程图(Flowcharts)功能,官方还推出了线框图(Wireframes).便利贴(Sticky Not ...