zip格式压缩和解压缩(支持中文和文件嵌套解压缩)

本文介绍了一种使用Java实现ZIP文件压缩和解压缩的方法。通过自定义代码,能够支持中文路径,并允许文件嵌套。文章提供了具体的实现代码及使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在公司项目过程需要用到zip文件的压缩与解压的功能,于是自己研究了一下,找了一些,自己写了一个压缩和解压缩的代码,支持中文,可以文件嵌套(注意其中所用的类是ant.jar中的包中的类,我用的是1.6.0版本)

/***//**
*用于对指定源文件路径下的所有文件压缩到指定的文件中
*
@paramdesString需要压缩成的文件名
*
@paramsrcString压缩文件源路径,可以是文件或文件夹
*
@returnboolean如果压缩成功返回true,否则表示失败
*/

publicstaticbooleanFile2Zip(Stringdes,Stringsrc)...{

booleansuccess=true;//压缩成功标志
Filesrcpath=newFile(src);
ZipOutputStreamout
=null;
try...{
out
=newZipOutputStream(newFileOutputStream(newFile(
des)));
//创建压缩输出流
out.setEncoding("gbk");
compress(srcpath,out,
"");//开始压缩
}

catch(FileNotFoundExceptionex)...{
success
=false;
}

catch(IOExceptionex1)...{
success
=false;
}

finally...{
if(out!=null)...{
try...{
out.close();
}

catch(IOExceptionex2)...{
}

}

}

returnsuccess;
}


/***//**
*
*
@paramsrcFile
*
@paramoutZipOutputStream
*
@parambaseString
*
@throwsIOException
*/

publicstaticvoidcompress(Filesrc,ZipOutputStreamout,Stringbase)throws
IOException
...{
if(src.isDirectory())...{//如果是目录
File[]files=src.listFiles();//列举出目录中所有的内容
out.putNextEntry(newZipEntry(base+"/"));//放入一级目录
base=base.length()==0?"":base+"/";
for(inti=0;i<files.length;i++)...{
compress(files[i],out,base
+files[i].getName());
}


}

else...{//如果是文件
if("".equals(base))...{
out.putNextEntry(
newZipEntry(base+"/"));
base
=src.getName();
}

out.putNextEntry(
newZipEntry(base));
FileInputStreamin
=newFileInputStream(src);
byte[]data=newbyte[4096];
intb;
while((b=in.read(data))!=-1)...{
out.write(data,
0,b);
}

in.close();

}

}

/***//**
*解压缩文件
*
@paramsrcStringzip所在路径c:/test/kk.zip
*
@paramdesString希望存放的目录
*
@throwsIOException
*
@throwsIOException
*/

publicstaticvoiddecompress(Stringsrc,Stringdes)throwsIOException
...{
ZipFilefile
=null;
FileOutputStreamfout
=null;
DataOutputStreamdout
=null;
try...{
file
=newZipFile(src);
InputStreamin
=null;
des
=des.replace('/','/');
if(des.startsWith("//"))...{
des
=des.replaceFirst("//","//");
}

Enumerationen
=file.getEntries();
while(en.hasMoreElements())...{
ZipEntryentry
=(ZipEntry)en.nextElement();
if(entry.isDirectory())...{//文件夹
Filedirectory=newFile(des+"/"+entry.getName());
if(!directory.exists())
directory.mkdirs();
}
else...{//文件
Stringpath=entry.getName();
path
=path.replace('/','/');
if(path.startsWith("//"))...{
path
=path.replaceFirst("//","//");
}

intpos=path.lastIndexOf('/');
if(pos!=-1)...{
path
=path.substring(0,pos+1);
Filed
=newFile(des+"/"+path);
if(!d.exists())
d.mkdirs();
}

try...{
Filefiles
=newFile(entry.getName());
Filef
=newFile(des+"/"+files.getPath());

fout
=newFileOutputStream(f);
dout
=newDataOutputStream(fout);
in
=file.getInputStream(entry);

byte[]b=newbyte[4096];
intlen=0;
while((len=in.read(b))!=-1)...{
dout.write(b,
0,len);
}


}
catch(IOExceptione)...{
throwe;
}
finally...{
if(fout!=null)
fout.close();
if(dout!=null)
dout.close();
if(in!=null)
in.close();
}

}

}

}
catch(IOExceptione)...{
throwe;
}
finally...{
if(file!=null)
file.close();
if(fout!=null)
fout.close();
if(dout!=null)
dout.close();
}

}

/***//**
*解压缩zip文件
*
@paramsrcString
*
@paramdesString
*
@returnboolean成功返回true;
*/

publicstaticbooleanzipDecompress(Stringsrc,Stringdes)...{
booleansuccess=true;
try...{
decompress(src,des);
}

catch(FileNotFoundExceptionex)...{
ex.printStackTrace();
success
=false;
}

catch(IOExceptionex)...{
ex.printStackTrace();
success
=false;
}


returnsuccess;
}

该类有很多不完善的地方,各位可以提意见,共同交流,谢谢

程序设计题二:ZIP格式压缩/解压系统设计 【问题描述】 由于网络带宽的限制,开发以互联网为传输媒介的软件系统时,在运行过程中的数据传输效率会成为评价一套软件系统性能的重要指标。由于网络的数据传输速度是软件运行的客观因素,因此,在这种情况下,程序设计人员首先考虑的减少软件系统运行过程中需要传输的数据量,如果有些数据必须要传输,则软件工程师通常将这些数据在发送端进行压缩,而在数据接收端将数据解压缩,从而主动减少应用系统数据传输量。 JDK环境中提供了多种类型的数据压缩方式,总结起来,利用Java语言可以创建的数据文件压缩格式包括如下类型:   ●ZIP格式   ●GZIP格式   ●JAR格式 通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似: 【实验目的】 要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件容器,理解java事件处理机制,会查看API documentation完成设计任务,熟练文件流的操作。 【基本功能】 (1) 通过菜单组件、按钮组件、文本框组件等完成创建ZIP压缩文件。 (2) ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作 (3) 功能的其他扩展 【指导建议】 完成实验指导书P97“Zip文件的读取与制作” 将程序进行修改,增加GUI设计,并完成基本功能, 其他功能扩展完善。 【程序设计的开发环境】 JDK1.5 及JCreator350/400。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值