youtube 视频地址-autocad教程下载

文件压缩器下载
2023年4月2日发(作者:cad2004注册机)

Java压缩⽂件⼯具类ZipUtil使⽤⽅法代码⽰例

本⽂实例通过Java的Zip输⼊输出流实现压缩和解压⽂件,前⼀部分代码实现获取⽂件路径,压缩⽂件名的更改等,具体如

下:

;

edInputStream;

edOutputStream;

;

putStream;

tputStream;

ption;

ry;

utStream;

putStream;

;

/**

*通过Java的Zip输⼊输出流实现压缩和解压⽂件

*

*@authorliujiduo

*

*/

publicfinalclassZipUtil{

privateZipUtil(){

//empty

}

/**

*压缩⽂件

*

*@paramfilePath

*待压缩的⽂件路径

*@return压缩后的⽂件

*/

publicstaticFilezip(StringfilePath){

Filetarget=null;

Filesource=newFile(filePath);

if(()){

//压缩⽂件名=源⽂件名.zip

StringzipName=e()+".zip";

target=newFile(ent(),zipName);

if(()){

();

//删除旧的⽂件

}

FileOutputStreamfos=null;

ZipOutputStreamzos=null;

try{

fos=newFileOutputStream(target);

zos=newZipOutputStream(newBufferedOutputStream(fos));

//添加对应的⽂件Entry

addEntry("/",source,zos);

}

catch(IOExceptione){

thrownewRuntimeException(e);

}

finally{

uietly(zos,fos);

}

}

returntarget;

}

/**

*扫描添加⽂件Entry

*

*@parambase

*基路径

*

*@paramsource

*源⽂件

*@paramzos

*Zip⽂件输出流

*@throwsIOException

*/

privatestaticvoidaddEntry(Stringbase,Filesource,ZipOutputStreamzos)

throwsIOException{

//按⽬录分级,形如:/aaa/

Stringentry=base+e();

if(ctory()){

for(Filefile:les()){

//递归列出⽬录下的所有⽂件,添加⽂件Entry

addEntry(entry+"/",file,zos);

}

}else{

FileInputStreamfis=null;

BufferedInputStreambis=null;

try{

byte[]buffer=newbyte[1024*10];

fis=newFileInputStream(source);

bis=newBufferedInputStream(fis,);

intread=0;

tEntry(newZipEntry(entry));

while((read=(buffer,0,))!=-1){

(buffer,0,read);

}

ntry();

}

finally{

uietly(bis,fis);

}

}

}

/**

*解压⽂件

*

*@paramfilePath

*压缩⽂件路径

*/

publicstaticvoidunzip(StringfilePath){

Filesource=newFile(filePath);

if(()){

ZipInputStreamzis=null;

BufferedOutputStreambos=null;

try{

zis=newZipInputStream(newFileInputStream(source));

ZipEntryentry=null;

while((entry=tEntry())!=null

&&!ctory()){

Filetarget=newFile(ent(),e());

if(!entFile().exists()){

//创建⽂件⽗⽬录

entFile().mkdirs();

}

//写⼊⽂件

bos=newBufferedOutputStream(newFileOutputStream(target));

intread=0;

byte[]buffer=newbyte[1024*10];

while((read=(buffer,0,))!=-1){

(buffer,0,read);

}

();

}

ntry();

}

catch(IOExceptione){

thrownewRuntimeException(e);

}

finally{

uietly(zis,bos);

}

}

}

publicstaticvoidmain(String[]args){

StringtargetPath="E:Win7壁纸";

Filefile=(targetPath);

n(file);

("F:Win7壁纸.zip");

}

}

下⾯是通过IO流⼯具类实现关闭⼀个或多个流对象的Java语⾔描述,获取可关闭的流对象列表,具体如下:

;

ble;

ption;

/**

*IO流⼯具类

*

*@authorliujiduo

*

*/

publicclassIOUtil{

/**

*关闭⼀个或多个流对象

*

*@paramcloseables

*可关闭的流对象列表

*@throwsIOException

*/

publicstaticvoidclose(Closeable...closeables)throwsIOException{

if(closeables!=null){

for(Closeablecloseable:closeables){

if(closeable!=null){

();

}

}

}

}

/**

*关闭⼀个或多个流对象

*

*@paramcloseables

*可关闭的流对象列表

*/

publicstaticvoidcloseQuietly(Closeable...closeables){

try{

close(closeables);

}

catch(IOExceptione){

//donothing

}

}

}

总结

以上就是本⽂关于Java压缩⽂件⼯具类ZipUtil使⽤⽅法代码⽰例的全部内容,希望对⼤家有所帮助。感兴趣的朋友可以继续

参阅本站其他相关专题,如有不⾜之处,欢迎留⾔指出。

更多推荐

文件压缩器下载