zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java解压zip文件示例

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);
    }
    }
}