Search in sources :

Example 1 with ZipFile

use of de.schlichtherle.util.zip.ZipFile in project otter by alibaba.

the class ArchiveBean method unpack.

public List<File> unpack(File archiveFile, File targetDir) throws ArchiveException {
    // 首先判断下对应的目标文件是否存在,如存在则执行删除
    if (false == archiveFile.exists()) {
        throw new ArchiveException(String.format("[%s] not exist", archiveFile.getAbsolutePath()));
    }
    if (false == targetDir.exists() && false == NioUtils.create(targetDir, false, 3)) {
        throw new ArchiveException(String.format("[%s] not exist and create failed", targetDir.getAbsolutePath()));
    }
    List<File> result = new ArrayList<File>();
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            // entry
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String entryName = entry.getName();
            // target
            File targetFile = new File(targetDir, entryName);
            // 尝试创建父路径
            NioUtils.create(targetFile.getParentFile(), false, 3);
            InputStream input = null;
            OutputStream output = null;
            try {
                output = new FileOutputStream(targetFile);
                input = zipFile.getInputStream(entry);
                NioUtils.copy(input, output);
            } finally {
                IOUtils.closeQuietly(input);
                IOUtils.closeQuietly(output);
            }
            result.add(targetFile);
        }
    } catch (Exception e) {
        throw new ArchiveException(e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ex) {
            }
        }
    }
    return result;
}
Also used : Enumeration(java.util.Enumeration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(de.schlichtherle.util.zip.ZipEntry) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ZipOutputStream(de.schlichtherle.util.zip.ZipOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) ZipFile(de.schlichtherle.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) ZipFile(de.schlichtherle.util.zip.ZipFile) File(java.io.File)

Aggregations

ZipEntry (de.schlichtherle.util.zip.ZipEntry)1 ZipFile (de.schlichtherle.util.zip.ZipFile)1 ZipOutputStream (de.schlichtherle.util.zip.ZipOutputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1