Java实现的zip工具类完整实例
本文实例讲述了Java实现的zip工具类。分享给大家供大家参考,具体如下:
实现把zip解压到指定路径,把文件夹压缩到zip,把文件列表压缩为zip的三个方法
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.nio.charset.Charset;
importjava.util.ArrayList;
importjava.util.Enumeration;
importjava.util.List;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
publicclassZipUtils
{
/**
*解压zip到指定路径
*@paramzipFile待解压文件
*@paramdescDir指定解压路径
*@returnfileNames解压的全部文件名
*@throwsIOException
*/
publicstaticListunZipFiles(FilezipFile,StringdescDir)throwsIOException
{
ListfileNames=newArrayList();
ZipFilezip=newZipFile(zipFile,Charset.forName("GBK"));//解决中文文件夹乱码
Stringname=zip.getName().substring(zip.getName().lastIndexOf('\\')+1,zip.getName().lastIndexOf('.'));
FilepathFile=newFile(descDir+name);
if(!pathFile.exists())
{
pathFile.mkdirs();
}
StringoutPath="";
for(Enumerationentries=zip.entries();entries.hasMoreElements();)
{
ZipEntryentry=(ZipEntry)entries.nextElement();
StringzipEntryName=entry.getName();
fileNames.add(zipEntryName);
InputStreamin=zip.getInputStream(entry);
outPath=(descDir+name+"/"+zipEntryName).replaceAll("\\*","/");
//判断路径是否存在,不存在则创建文件路径
Filefile=newFile(outPath.substring(0,outPath.lastIndexOf('/')));
if(!file.exists())
{
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(newFile(outPath).isDirectory())
{
continue;
}
//输出文件路径信息
FileOutputStreamout=newFileOutputStream(outPath);
byte[]buf1=newbyte[1024];
intlen;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
pathFile.delete();
returnfileNames;
}
/**
*压缩文件夹成zip
*@paramsrcDir待打包的文件夹路径
*@paramout打包文件名及存储路径
*@paramKeepDirStructure是否保留文件夹结构不保留则把文件夹下全部文件都打压在一起
*@throwsRuntimeException
*/
publicstaticvoiddocToZip(StringsrcDir,OutputStreamout,booleanKeepDirStructure)throwsRuntimeException
{
longstart=System.currentTimeMillis();
ZipOutputStreamzos=null;
try
{
zos=newZipOutputStream(out);
FilesourceFile=newFile(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
longend=System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
}catch(Exceptione)
{
thrownewRuntimeException("ziperrorfromZipUtils",e);
}finally
{
if(zos!=null)
{
try
{
zos.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
}
/**
*压缩成ZIP将多个文件大包
*@paramsrcFiles需要压缩的文件列表
*@paramout压缩文件输出流
*@throwsRuntimeException压缩失败会抛出运行时异常
*/
publicstaticvoidfilesToZip(ListsrcFiles,OutputStreamout)throwsRuntimeException
{
longstart=System.currentTimeMillis();
ZipOutputStreamzos=null;
intBUFFER_SIZE=2*1024;
try
{
zos=newZipOutputStream(out);
for(FilesrcFile:srcFiles)
{
byte[]buf=newbyte[BUFFER_SIZE];
zos.putNextEntry(newZipEntry(srcFile.getName()));
intlen;
FileInputStreamin=newFileInputStream(srcFile);
while((len=in.read(buf))!=-1)
{
zos.write(buf,0,len);
}
zos.closeEntry();
in.close();
}
longend=System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
}catch(Exceptione)
{
thrownewRuntimeException("ziperrorfromZipUtils",e);
}finally
{
if(zos!=null)
{
try
{
zos.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
}
/**
*递归压缩方法
*@paramsourceFile源文件
*@paramzoszip输出流
*@paramname压缩后的名称
*@paramKeepDirStructure是否保留原来的目录结构,true:保留目录结构;
*false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
*@throwsException
*/
privatestaticvoidcompress(FilesourceFile,ZipOutputStreamzos,Stringname,
booleanKeepDirStructure)throwsException
{
intBUFFER_SIZE=2*1024;
byte[]buf=newbyte[BUFFER_SIZE];
if(sourceFile.isFile())
{
//向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(newZipEntry(name));
//copy文件到zip输出流中
intlen;
FileInputStreamin=newFileInputStream(sourceFile);
while((len=in.read(buf))!=-1)
{
zos.write(buf,0,len);
}
//Completetheentry
zos.closeEntry();
in.close();
}else
{
File[]listFiles=sourceFile.listFiles();
if(listFiles==null||listFiles.length==0)
{
//需要保留原来的文件结构时,需要对空文件夹进行处理
if(KeepDirStructure)
{
//空文件夹的处理
zos.putNextEntry(newZipEntry(name+"/"));
//没有文件,不需要文件的copy
zos.closeEntry();
}
}else
{
for(Filefile:listFiles)
{
//判断是否需要保留原来的文件结构
if(KeepDirStructure)
{
//注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
//不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file,zos,name+"/"+file.getName(),KeepDirStructure);
}else
{
compress(file,zos,file.getName(),KeepDirStructure);
}
}
}
}
}
}
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。