Search in sources :

Example 1 with ZipFile

use of org.apache.tools.zip.ZipFile in project gradle by gradle.

the class ZipFileTree method visit.

public void visit(FileVisitor visitor) {
    if (!zipFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!zipFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }
    AtomicBoolean stopFlag = new AtomicBoolean();
    try {
        ZipFile zip = new ZipFile(zipFile);
        try {
            // The iteration order of zip.getEntries() is based on the hash of the zip entry. This isn't much use
            // to us. So, collect the entries in a map and iterate over them in alphabetical order.
            Map<String, ZipEntry> entriesByName = new TreeMap<String, ZipEntry>();
            Enumeration entries = zip.getEntries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                entriesByName.put(entry.getName(), entry);
            }
            Iterator<ZipEntry> sortedEntries = entriesByName.values().iterator();
            while (!stopFlag.get() && sortedEntries.hasNext()) {
                ZipEntry entry = sortedEntries.next();
                if (entry.isDirectory()) {
                    visitor.visitDir(new DetailsImpl(entry, zip, stopFlag, chmod));
                } else {
                    visitor.visitFile(new DetailsImpl(entry, zip, stopFlag, chmod));
                }
            }
        } finally {
            zip.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Enumeration(java.util.Enumeration) ZipFile(org.apache.tools.zip.ZipFile) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ZipEntry(org.apache.tools.zip.ZipEntry) GradleException(org.gradle.api.GradleException) TreeMap(java.util.TreeMap) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException)

Example 2 with ZipFile

use of org.apache.tools.zip.ZipFile in project libresonic by Libresonic.

the class UploadController method unzip.

private void unzip(File file, List<File> unzippedFiles) throws Exception {
    LOG.info("Unzipping " + file);
    ZipFile zipFile = new ZipFile(file);
    try {
        Enumeration<?> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File entryFile = new File(file.getParentFile(), entry.getName());
            if (!entry.isDirectory()) {
                if (!securityService.isUploadAllowed(entryFile)) {
                    throw new Exception("Permission denied: " + StringUtil.toHtml(entryFile.getPath()));
                }
                entryFile.getParentFile().mkdirs();
                InputStream inputStream = null;
                OutputStream outputStream = null;
                try {
                    inputStream = zipFile.getInputStream(entry);
                    outputStream = new FileOutputStream(entryFile);
                    byte[] buf = new byte[8192];
                    while (true) {
                        int n = inputStream.read(buf);
                        if (n == -1) {
                            break;
                        }
                        outputStream.write(buf, 0, n);
                    }
                    LOG.info("Unzipped " + entryFile);
                    unzippedFiles.add(entryFile);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
        zipFile.close();
        file.delete();
    } finally {
        zipFile.close();
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(org.apache.tools.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 3 with ZipFile

use of org.apache.tools.zip.ZipFile in project tdi-studio-se by Talend.

the class ZipFileUtils method unZipFile.

private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry) throws IOException {
    InputStream inputStream;
    FileOutputStream fileOut;
    if (entry.isDirectory()) {
        destFile.mkdirs();
    } else {
        File parent = destFile.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        inputStream = zipFile.getInputStream(entry);
        fileOut = new FileOutputStream(destFile);
        byte[] buf = new byte[bufSize];
        int readedBytes;
        while ((readedBytes = inputStream.read(buf)) > 0) {
            fileOut.write(buf, 0, readedBytes);
        }
        fileOut.close();
        inputStream.close();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 4 with ZipFile

use of org.apache.tools.zip.ZipFile in project tdi-studio-se by Talend.

the class ZipFileUtils method unZip.

public static String unZip(String unZipFileName, String destFileName) {
    File unzipFile = new File(unZipFileName);
    if (destFileName == null || destFileName.trim().length() == 0) {
        destFileName = unzipFile.getParent();
    }
    File destFile;
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(unzipFile, "GBK");
        for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            destFile = new File(destFileName, entry.getName());
            unZipFile(destFile, zipFile, entry);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        return e.getMessage();
    } finally {
        try {
            assert zipFile != null;
            zipFile.close();
        } catch (Exception e) {
            ExceptionHandler.process(e);
        }
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) IOException(java.io.IOException)

Aggregations

ZipFile (org.apache.tools.zip.ZipFile)4 File (java.io.File)3 ZipEntry (org.apache.tools.zip.ZipEntry)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Enumeration (java.util.Enumeration)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 TreeMap (java.util.TreeMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 GradleException (org.gradle.api.GradleException)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1