zip读取

packagecom.zhey.decompression;

importorg.apache.commons.compress.archivers.zip.ZipArchiveEntry;

importorg.apache.commons.compress.archivers.zip.ZipEncoding;

importorg.apache.commons.compress.archivers.zip.ZipEncodingHelper;

importorg.apache.commons.compress.archivers.zip.ZipFile;

importjava.io.*;

importjava.util.ArrayList;

importjava.util.Enumeration;

importjava.util.List;

/**

* 获取zip文件的详细目录结构

* 解压指定的文件

*

*@authorzhey

*/

publicclassZipextendsAbastractCompressimplementsCompression{

private ZipFilezip File;

@Override

publicbooleanloadCompress(Filefile) {

Listfiles=newArrayList();

try{

zipFile=newZipFile(file,"CP936");

EnumerationzipEntryEnumeration=zipFile.getEntries();

while(zipEntryEnumeration.hasMoreElements()) {

ZipArchiveEntryzipEntry=zipEntryEnumeration.nextElement();

if(zipEntry.isDirectory()) {

addPath(zipEntry.getName());

}else{

addFile(zipEntry.getName());

                }

            }

returntrue;

}catch(IOExceptione) {

        }

returnfalse;

    }

@Override

protectedbooleanextract(Stringfilepath,StringdestPath) {

try{

ZipArchiveEntryentry=(ZipArchiveEntry) zipFile.getEntry(filepath);

if(entry==null) {

returnfalse;

}else{

if(entry.isDirectory()) {

EnumerationzipArchiveEntries=zipFile.getEntries();

while(zipArchiveEntries.hasMoreElements()) {

ZipArchiveEntrytmpEntry=zipArchiveEntries.nextElement();

if(tmpEntry.getName().startsWith(filepath)&&!tmpEntry.isDirectory()) {

StringfileName=tmpEntry.getName();

fileName=fileName.substring(filepath.length(), fileName.length());

                            writeToFile(tmpEntry, fileName, destPath);

                        }

                    }

}else{

//解压文件

StringsrcFileName=filepath.substring(filepath.lastIndexOf(File.separator), filepath.length());

                    writeToFile(entry, srcFileName, destPath);

                }

            }

returntrue;

}catch(FileNotFoundExceptione) {

e.printStackTrace();

}catch(IOExceptione) {

e.printStackTrace();

        }

returnfalse;

    }

@Override

protectedbooleanextractFile(Stringfilepath,StringdestPath,StringnewName) {

try{

ZipArchiveEntryentry=(ZipArchiveEntry) zipFile.getEntry(filepath);

if(entry==null) {

returnfalse;

}else{

if(entry.isDirectory()) {

returnfalse;

}else{

//解压文件

                    writeToFile(entry, newName, destPath);

                }

            }

returntrue;

}catch(FileNotFoundExceptione) {

e.printStackTrace();

}catch(IOExceptione) {

e.printStackTrace();

        }

returnfalse;

    }

privatevoidwriteToFile(ZipArchiveEntryentry,StringsrcFileName,StringdestPath)throwsIOException{

InputStreaminputStream=zipFile.getInputStream(entry);

OutputStreamoutputStream;

FiledestDir=newFile(destPath);

if(!destDir.exists()) {

destDir.mkdir();

        }

if(destPath.endsWith(File.separator)) {

destPath+=srcFileName;

}else{

destPath=destPath+File.separator+srcFileName;

        }

Filefile=newFile(destPath);

Filedir=file.getParentFile();

if(!dir.exists())

dir.mkdirs();

outputStream=newFileOutputStream(destPath);

byte[] buffer=newbyte[1024];

intsize;

while((size=inputStream.read(buffer))>0) {

outputStream.write(buffer,0, size);

        }

outputStream.close();

inputStream.close();

    }

@Override

publicvoidclose() {

try{

if(zipFile!=null)

zipFile.close();

}catch(IOExceptione) {

e.printStackTrace();

        }

    }

}

你可能感兴趣的:(zip读取)