使用java API实现zip递归压缩和解压文件夹
一、概述
在本篇文章中,给大家介绍一下如何将文件进行zip压缩以及如何对zip包解压。所有这些都是使用Java提供的核心库java.util.zip来实现的。
二、压缩文件
首先我们来学习一个简单的例子-压缩单个文件。将一个名为test1.txt的文件压缩到一个名为Compressed.zip的zip文件中。
publicclassZipFile{
publicstaticvoidmain(String[]args)throwsIOException{
//输出压缩包
FileOutputStreamfos=newFileOutputStream("src/main/resources/compressed.zip");
ZipOutputStreamzipOut=newZipOutputStream(fos);
//被压缩文件
FilefileToZip=newFile("src/main/resources/test1.txt");
FileInputStreamfis=newFileInputStream(fileToZip);
//向压缩包中添加文件
ZipEntryzipEntry=newZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[]bytes=newbyte[1024];
intlength;
while((length=fis.read(bytes))>=0){
zipOut.write(bytes,0,length);
}
zipOut.close();
fis.close();
fos.close();
}
}
三、压缩多个文件
接下来,我们看看如何将多个文件压缩为一个zip文件。我们将把test1.txt和test2.txt压缩成multiCompressed.zip:
publicclassZipMultipleFiles{
publicstaticvoidmain(String[]args)throwsIOException{
ListsrcFiles=Arrays.asList("src/main/resources/test1.txt","src/main/resources/test2.txt");
FileOutputStreamfos=newFileOutputStream("src/main/resources/multiCompressed.zip");
ZipOutputStreamzipOut=newZipOutputStream(fos);
//向压缩包中添加多个文件
for(StringsrcFile:srcFiles){
FilefileToZip=newFile(srcFile);
FileInputStreamfis=newFileInputStream(fileToZip);
ZipEntryzipEntry=newZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[]bytes=newbyte[1024];
intlength;
while((length=fis.read(bytes))>=0){
zipOut.write(bytes,0,length);
}
fis.close();
}
zipOut.close();
fos.close();
}
}
四、压缩目录
下面的例子,我们将zipTest目录及该目录下的递归子目录文件,全都压缩到dirCompressed.zip中:
publicclassZipDirectory{
publicstaticvoidmain(String[]args)throwsIOException,FileNotFoundException{
//被压缩的文件夹
StringsourceFile="src/main/resources/zipTest";
//压缩结果输出,即压缩包
FileOutputStreamfos=newFileOutputStream("src/main/resources/dirCompressed.zip");
ZipOutputStreamzipOut=newZipOutputStream(fos);
FilefileToZip=newFile(sourceFile);
//递归压缩文件夹
zipFile(fileToZip,fileToZip.getName(),zipOut);
//关闭输出流
zipOut.close();
fos.close();
}
/**
*将fileToZip文件夹及其子目录文件递归压缩到zip文件中
*@paramfileToZip递归当前处理对象,可能是文件夹,也可能是文件
*@paramfileNamefileToZip文件或文件夹名称
*@paramzipOut压缩文件输出流
*@throwsIOException
*/
privatestaticvoidzipFile(FilefileToZip,StringfileName,ZipOutputStreamzipOut)throwsIOException{
//不压缩隐藏文件夹
if(fileToZip.isHidden()){
return;
}
//判断压缩对象如果是一个文件夹
if(fileToZip.isDirectory()){
if(fileName.endsWith("/")){
//如果文件夹是以“/”结尾,将文件夹作为压缩箱放入zipOut压缩输出流
zipOut.putNextEntry(newZipEntry(fileName));
zipOut.closeEntry();
}else{
//如果文件夹不是以“/”结尾,将文件夹结尾加上“/”之后作为压缩箱放入zipOut压缩输出流
zipOut.putNextEntry(newZipEntry(fileName+"/"));
zipOut.closeEntry();
}
//遍历文件夹子目录,进行递归的zipFile
File[]children=fileToZip.listFiles();
for(FilechildFile:children){
zipFile(childFile,fileName+"/"+childFile.getName(),zipOut);
}
//如果当前递归对象是文件夹,加入ZipEntry之后就返回
return;
}
//如果当前的fileToZip不是一个文件夹,是一个文件,将其以字节码形式压缩到压缩包里面
FileInputStreamfis=newFileInputStream(fileToZip);
ZipEntryzipEntry=newZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[]bytes=newbyte[1024];
intlength;
while((length=fis.read(bytes))>=0){
zipOut.write(bytes,0,length);
}
fis.close();
}
}
- 要压缩子目录及其子目录文件,所以需要递归遍历
- 每次遍历找到的是目录时,我们都将其名称附加“/”,并将其以ZipEntry保存到压缩包中,从而保持压缩的目录结构。
- 每次遍历找到的是文件时,将其以字节码形式压缩到压缩包里面
五、解压缩zip压缩包
下面为大家举例讲解解压缩zip压缩包。在此示例中,我们将compressed.zip解压缩到名为unzipTest的新文件夹中。
publicclassUnzipFile{
publicstaticvoidmain(String[]args)throwsIOException{
//被解压的压缩文件
StringfileZip="src/main/resources/unzipTest/compressed.zip";
//解压的目标目录
FiledestDir=newFile("src/main/resources/unzipTest");
byte[]buffer=newbyte[1024];
ZipInputStreamzis=newZipInputStream(newFileInputStream(fileZip));
//获取压缩包中的entry,并将其解压
ZipEntryzipEntry=zis.getNextEntry();
while(zipEntry!=null){
FilenewFile=newFile(destDir,zipEntry);
FileOutputStreamfos=newFileOutputStream(newFile);
intlen;
while((len=zis.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
//解压完成一个entry,再解压下一个
zipEntry=zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
//在解压目标文件夹,新建一个文件
publicstaticFilenewFile(FiledestinationDir,ZipEntryzipEntry)throwsIOException{
FiledestFile=newFile(destinationDir,zipEntry.getName());
StringdestDirPath=destinationDir.getCanonicalPath();
StringdestFilePath=destFile.getCanonicalPath();
if(!destFilePath.startsWith(destDirPath+File.separator)){
thrownewIOException("该解压项在目标文件夹之外:"+zipEntry.getName());
}
returndestFile;
}
}
总结
到此这篇关于使用javaAPI实现zip递归压缩文件夹及解压的文章就介绍到这了,更多相关javaAPI实现zip递归压缩文件夹及解压内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。