java解压zip文件示例
2023-06-13 09:15:18 时间
若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Windows系统中文件名是以GBK方式编码的,所以如果是解压一个包含中文文件名的zip包,会报非法参数异常,所以要实现解压缩,就得对DeflaterOutputStream.java、InflaterInputStream.java、ZipConstants.java、ZipEntry.java、ZipInputStream.java以及ZipOutputStream.java这些相关的类进行修改,过程如下:
因为从J2SE1.4开始,Java编译器不再支持import进未命包名的类、接口,所以在创建的Java项目中,一定要新建一个自己定义的包,包命名的格式一般为学校域名的逆序+自己的网名,比如cn.edu.xidian.crytoll。
在包内新建DeflaterOutputStream类,代码如下:
DeflaterOutputStream.java:
packagecn.edu.xdian.crytoll;
importjava.io.FilterOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.zip.Deflater;
/**
*Thisclassimplementsanoutputstreamfilterforcompressingdatain
*the"deflate"compressionformat.Itisalsousedasthebasisforother
*typesofcompressionfilters,suchasGZIPOutputStream.
*
*@see Deflater
*@version 1.36,03/13/06
*@author DavidConnelly
*/
public
classDeflaterOutputStreamextendsFilterOutputStream{
/**
*Compressorforthisstream.
*/
protectedDeflaterdef;
/**
*Outputbufferforwritingcompresseddata.
*/
protectedbyte[]buf;
/**
*Indicatesthatthestreamhasbeenclosed.
*/
privatebooleanclosed=false;
/**
*Createsanewoutputstreamwiththespecifiedcompressorand
*buffersize.
*@paramouttheoutputstream
*@paramdefthecompressor("deflater")
*@paramsizetheoutputbuffersize
*@exceptionIllegalArgumentExceptionifsizeis<=0
*/
publicDeflaterOutputStream(OutputStreamout,Deflaterdef,intsize){
super(out);
if(out==null||def==null){
thrownewNullPointerException();
}elseif(size<=0){
thrownewIllegalArgumentException("buffersize<=0");
}
this.def=def;
buf=newbyte[size];
}
/**
*Createsanewoutputstreamwiththespecifiedcompressorand
*adefaultbuffersize.
*@paramouttheoutputstream
*@paramdefthecompressor("deflater")
*/
publicDeflaterOutputStream(OutputStreamout,Deflaterdef){
this(out,def,512);
}
booleanusesDefaultDeflater=false;
/**
*Createsanewoutputstreamwithadefaultcompressorandbuffersize.
*@paramouttheoutputstream
*/
publicDeflaterOutputStream(OutputStreamout){
this(out,newDeflater());
usesDefaultDeflater=true;
}
/**
*Writesabytetothecompressedoutputstream.Thismethodwill
*blockuntilthebytecanbewritten.
*@parambthebytetobewritten
*@exceptionIOExceptionifanI/Oerrorhasoccurred
*/
publicvoidwrite(intb)throwsIOException{
byte[]buf=newbyte[1];
buf[0]=(byte)(b&0xff);
write(buf,0,1);
}
/**
*Writesanarrayofbytestothecompressedoutputstream.This
*methodwillblockuntilallthebytesarewritten.
*@parambthedatatobewritten
*@paramoffthestartoffsetofthedata
*@paramlenthelengthofthedata
*@exceptionIOExceptionifanI/Oerrorhasoccurred
*/
publicvoidwrite(byte[]b,intoff,intlen)throwsIOException{
if(def.finished()){
thrownewIOException("writebeyondendofstream");
}
if((off|len|(off+len)|(b.length-(off+len)))<0){
thrownewIndexOutOfBoundsException();
}elseif(len==0){
return;
}
if(!def.finished()){
//Deflatenomorethanstridebytesatatime. Thisavoids
//excesscopyingindeflateBytes(seeDeflater.c)
intstride=buf.length;
for(inti=0;i<len;i+=stride){
def.setInput(b,off+i,Math.min(stride,len-i));
while(!def.needsInput()){
deflate();
}
}
}
}
/**
*Finisheswritingcompresseddatatotheoutputstreamwithoutclosing
*theunderlyingstream.Usethismethodwhenapplyingmultiplefilters
*insuccessiontothesameoutputstream.
*@exceptionIOExceptionifanI/Oerrorhasoccurred
*/
publicvoidfinish()throwsIOException{
if(!def.finished()){
def.finish();
while(!def.finished()){
deflate();
}
}
}
/**
*Writesremainingcompresseddatatotheoutputstreamandclosesthe
*underlyingstream.
*@exceptionIOExceptionifanI/Oerrorhasoccurred
*/
publicvoidclose()throwsIOException{
if(!closed){
finish();
if(usesDefaultDeflater)
def.end();
out.close();
closed=true;
}
}
/**
*Writesnextblockofcompresseddatatotheoutputstream.
*@throwsIOExceptionifanI/Oerrorhasoccurred
*/
protectedvoiddeflate()throwsIOException{
intlen=def.deflate(buf,0,buf.length);
if(len>0){
out.write(buf,0,len);
}
}
}
相关文章
- java强制删文件夹_Java 删除文件夹 和 文件 集合
- java pfx_如何在Java中读取.pfx文件的内容?
- java源程序文件的扩展名_使用Java语言编写的源程序保存时的文件扩展名是什么…
- java代码大全及详解_Java练级攻略[通俗易懂]
- java技术介绍_Java技术汇总
- Java类的初始化顺序_java静态类初始化顺序
- java文件转码工具-native2ascii.exe命令简介
- java 自定义类加载器_JAVA中如何使用应用自定义类加载器「建议收藏」
- java解析zip文件_java解压zip文件
- Java将图片Base64编码转换成img图片文件
- 【Java 虚拟机原理】Class 字节码二进制文件分析 三 ( 访问和修饰标志 | 类索引 | 父类索引 | 接口计数器 | 接口表 | 字段计数器 | 字段表 )
- Java实现FTP文件上传详解编程语言
- 文件或是文件夹打包压缩成zip格式的Java代码详解编程语言
- java使用itext为pdf文件设置密码保护详解编程语言
- 深入理解Java虚拟机 – 类文件结构详解编程语言
- Java在Linux系统上的安装(java安装linux)
- Linux下增加Zip文件的实现方法(linux给zip加文件)
- Linux下解压Zip文件的简便方式(zip怎么linux解压)
- 解压Linux中的ZIP文件:快速简便(linux解压zip命令)
- Java轻松使用Redis实现数据高效存储(java使用redis)
- 快速上手:Java连接Mongodb数据库(java连接mongodb数据库)
- Java与MySQL共同构建良好的数据表(java mysql 表)
- Java解答Oracle使用更轻松的实现方式(oracle写成java)
- Java加速Oracle开发之旅(java中oracle包)
- MySQL下载教程如何获取ZIP文件版本(mysql下载是zip)
- MySQL怎样使用Zip文件(mysql zip怎么用)
- java开发之读写txt文件操作的实现