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. 用Access作为后台数据库支撑,书写一个用C#写入记录的案例

    具体的步骤: 1.创建并打开一个OleDbConnection对象 2.创建插入的SQL语句 3.创建一个OleDbCommand对象 4.使用OleDbCommand对象来插入数据 5.关闭OleD ...

  2. 基于jquery的消息提示框toastr.js

    //消息提示全局配置 toastr.options = { "closeButton": false,//是否配置关闭按钮 "debug": false,//是 ...

  3. http协议(一)基础知识

    我自己写的随笔一般是偏学习笔记性质的,或者一点个人理解,适合新人,大牛可以忽略这个...... 参考书籍——<图解http> 当我们在浏览器的地址栏中输入网址,然后点击回车,接着,浏览器就 ...

  4. 【转】【MySql】mysql存储过程中的异常处理

    定义异常捕获类型及处理方法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement ...

  5. Kafka是分布式发布-订阅消息系统

    Kafka是分布式发布-订阅消息系统 https://www.biaodianfu.com/kafka.html Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apa ...

  6. Framework/base 下添加自定义模块的步骤

    在Android源码编译成功的基础上,重新编译带自己API的android.jar需要进行以下几个步骤操作:1.添加自己的源代码,在android源码的frameworks/base目录下新建一个文件 ...

  7. Windows 8的本地化应用程序清单

    I need to localize some data in application manifest (like name, description, splashscreen images et ...

  8. React Native 中组件的生命周期

    概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...

  9. vbs外部调用

    一.QTP调用外部VBS的方法 加到QTP的Resource中 在QTP菜单中设置, 菜单FileàSettingsàResource,将要加载的VB脚本添加进来. 举例: 步骤1:在D盘下新建一个V ...

  10. CentOs中mysql的安装与配置

    在linux中安装数据库首选MySQL,Mysql数据库的第一个版本就是发行在Linux系统上,其他选择还可以有postgreSQL,oracle等 在Linux上安装mysql数据库,我们可以去其官 ...