zip扮演着归档和压缩两个角色;gzip并不将文件归档,仅只是对单个文件进行压缩,所以,在UNIX平台上,命令tar通常用来创建一个档案文件,然后命令gzip来将档案文件压缩。

Java I/O类库还收录了一些能读写压缩格式流的类。要想提供压缩功能,只要把它们包在已有的I/O类的外面就行了。这些类不是Reader和Writer,而是InputStream和OutStreamput的子类。这是因为压缩算法是针对byte而不是字符的。

需要注意的是:java自带的工具类在windows压缩处理编码无法处理中文,所以不建议使用jre

相关类与接口:

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 文件读取条目

压缩类的用法非常简单;只要用GZIPOutputStream 或ZipOutputStream把输出流包起来,再用GZIPInputStream 或ZipInputStream把输入流包起来就行了。剩下的都是些普通的I/O操作。

测试

package com.jre.util.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import com.jre.io.UtilsIoJre; /**
* zip文件处理(压缩处理时通过windows系统压缩的由于编码不是utf8的所以将导致中文抛出异常的情况)
* @author huage
*
*/
public class UtilsZipJre { public static void main(String[] args) {
zipFileExtract("C:\\Users\\huage\\Desktop\\test\\111\\111.zip","C:\\Users\\huage\\Desktop\\test\\test");
//zipDecompressingExtract("C:\\Users\\huage\\Desktop\\test\\111\\111.zip","C:\\Users\\huage\\Desktop\\test\\test");
//zipCompressingExtract("C:\\Users\\huage\\Desktop\\test\\111\\test.zip",new File("C:\\Users\\huage\\Desktop\\test\\test")); } /**
* 解压
* @param path
* @param pathExtract
*/
public static void zipFileExtract(String path, String pathExtract){
ZipFile zipfile = null;
try {
zipfile = new ZipFile(path);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zipfile.entries();
if( entries!=null ){
ZipEntry entry ;
File file;
BufferedInputStream bis = null;
while( entries.hasMoreElements()){
entry = entries.nextElement();
if(entry.isDirectory())continue;
file=new File(pathExtract,entry.getName());
if(!file.exists()){
(new File(file.getParent())).mkdirs();
}
UtilsIoJre.converWriteIO(bis, file);
//System.out.println(fout+"解压成功");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally{
UtilsIoJre.closeIO(zipfile);
} } /**
* zip解压(本地)(官方自带zip解压无法处理中文)
*
* @param path
* :zip文件地址
* @param pathExtract
* :解压地址
*/
public static void zipDecompressingExtract(String path, String pathExtract) {
ZipInputStream zipinput = null;
BufferedInputStream bininput = null;
try {
zipinput = new ZipInputStream(new FileInputStream(path));
bininput = new BufferedInputStream(zipinput); ZipEntry entry ;
File fout = null;
while ((entry = zipinput.getNextEntry()) != null) {
if(entry.isDirectory())continue;
fout=new File(pathExtract,entry.getName());
if(!fout.exists()){
(new File(fout.getParent())).mkdirs();
}
UtilsIoJre.converWriteIO(bininput, fout);
//System.out.println(fout+"解压成功");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
UtilsIoJre.closeIO(bininput,zipinput);
}
System.out.println("解压完成");
} /**
* zip压缩(本地)
*
* @param zipFileName
* @param inputFile
* @throws Exception
*/
public static void zipCompressingExtract(String zipFileName, File inputFile) {
ZipOutputStream out = null;
BufferedOutputStream bo = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFileName));
bo = new BufferedOutputStream(out);
zipCompressing(out, inputFile, bo);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
UtilsIoJre.closeIO(bo,out);
}
System.out.println("压缩完成");
} /**
* zip压缩
*
* @param out
* @param file
* @param base
* @param bo
* @throws Exception
*/
private static void zipCompressing(ZipOutputStream out, File file, BufferedOutputStream bo) throws Exception {
if (file.isDirectory()) {
File[] fl = file.listFiles();
if (fl.length == 0) {
out.putNextEntry(new ZipEntry(file.getName()));
}
for (int i = 0; i < fl.length; i++) {
zipCompressing(out, fl[i], bo);
}
} else {
out.putNextEntry(new ZipEntry(file.getName() ));
UtilsIoJre.converReadIO(bo, file);
}
}
} package com.jre.io; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; /**
* io流处理
* @author huage
*
*/
public class UtilsIoJre { /**
* 将file写入BufferedOutputStream中
* @param bo
* @param file
* @throws Exception
*/
public static void converReadIO(BufferedOutputStream bo,File file) throws Exception{
FileInputStream in = new FileInputStream(file);
BufferedInputStream bi = new BufferedInputStream(in);
int b;
while ((b = in.read()) != -1) {
bo.write(b);
}
closeIO(bi,in);
bo.flush();//清空缓存
} /**
* 将BufferedInputStream写入file中
* @param bo
* @param file
* @throws Exception
*/
public static void converWriteIO(BufferedInputStream bininput,File file) throws Exception{
FileOutputStream out = new FileOutputStream(file);
BufferedOutputStream bout = new BufferedOutputStream(out);
int b;
while ((b = bininput.read()) != -1) {
bout.write(b);
}
closeIO(bout,out);
bout.flush();//清空缓存
} /**
* 关闭io
* @param cl
*/
public static void closeIO(AutoCloseable... cl){
if( cl == null || cl.length == 0 )return;
for (AutoCloseable c : cl) {
try {
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

java-a实现压缩与解压缩(zip、gzip)的更多相关文章

  1. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  2. Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)

    本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...

  3. ICSharpCode.SharpZipLib工具压缩与解压缩zip文件

    using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...

  4. java 版本压缩、解压缩zip

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  5. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  6. java压缩/解压缩zip格式文件

    因为项目要用到压缩.解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的.基本上还是比较好用的. 废话少说,直接上代码. }     ...

  7. Java 基础【12】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  8. Java 基础【15】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  9. linux zip命令 tar命令 【压缩、解压缩】参数列表:

    linux zip命令参数列表:   -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 -h 显示帮助界面 -m 将文件压缩之后,删除源文件   -n 特定字符串 不压缩具有特定字尾字符串 ...

  10. Java生成压缩文件(zip、rar 格式)

    jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

随机推荐

  1. guava常用操作

    Jack47 我思故我在 Google Java编程库Guava介绍 本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含 ...

  2. NOIP模拟赛 最大匹配

    问题描述 mhy12345学习了二分图匹配,二分图是一种特殊的图,其中的点可以分到两个集合中,使得相同的集合中的点两两没有连边.     图的“匹配”是指这个图的一个边集,里面的边两两不存在公共端点. ...

  3. FFT的物理意义

    来源:学步园 FFT(Fast Fourier Transform,快速傅立叶变换)是离散傅立叶变换的快速算法,也是我们在数字信号处理技术中经常会提到的一个概念.在大学的理工科课程中,在完成高等数学的 ...

  4. this.down和this.up用法

    down既可以加id也可以加xtype

  5. NOI2018准备Day7

    昨天没写,就不补了. 晚上追剧到3点,今天困死...... 上午做了一道水题,然后找一个程序的神奇的错误花了3个小时 下午做了3道递归吧,稍微难一点儿的黄金题就卡了 刚开始学递归时没多做题练练,现在 ...

  6. 利用ThinkPHP自带的七牛云驱动上传文件到七牛云以及删除七牛云文件方法

    一.准备工作 1.注册七牛云账号 2.选择对象储存->创建空间->设置为公开 3.在config配置文件中添加以下代码 'UPLOAD_FILE_QINIU' => array ( ...

  7. Reverse Words in a String

    void reverseWords(string &s) { string res = "", tmp = ""; int l = s.length() ...

  8. javascript 位运算

    位运算博大精深,本文总结下基本的位运算的概念. 1.整数的二进制码 位操作符用于在最基本的层次上,即按内存中表示数值的位来操作数值.ECMAScript中的所有数值都以IEEE-754 64位格式存储 ...

  9. 用c#操作Mongodb(附demo)

    因为需要,写了一个基于泛型的helper,这样要使用起来方便一点. 为了大家也不重复造轮子,所以发出来希望能帮到谁. 复杂的查询最好用linq,这也是mongodb官方建议的. mongodb的C#配 ...

  10. ASP.NET 系列:单元测试之StructureMap

    ASP.NET使用StructureMap等依赖注入组件时最重要就是EntityFramework的DbContext对象要保证在每次HttpRequest只有一个DbContext实例,这里将使用第 ...