Search in sources :

Example 11 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project buck by facebook.

the class ZipStepTest method minCompressionWritesCorrectZipFile.

/**
   * Tests a couple bugs:
   *     1) {@link com.facebook.buck.zip.OverwritingZipOutputStream} was writing uncompressed zip
   *        entries incorrectly.
   *     2) {@link ZipStep} wasn't setting the output size when writing uncompressed entries.
   */
@Test
public void minCompressionWritesCorrectZipFile() throws IOException {
    Path parent = tmp.newFolder("zipstep");
    Path out = parent.resolve("output.zip");
    Path toZip = tmp.newFolder("zipdir");
    byte[] contents = "hello world".getBytes();
    Files.write(toZip.resolve("file1.txt"), contents);
    Files.write(toZip.resolve("file2.txt"), contents);
    Files.write(toZip.resolve("file3.txt"), contents);
    ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
    // directory and will verify it's valid.
    try (ZipFile zip = new ZipFile(out.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        ZipArchiveEntry entry1 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry1)));
        ZipArchiveEntry entry2 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry2)));
        ZipArchiveEntry entry3 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry3)));
    }
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Example 12 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project ats-framework by Axway.

the class LocalFileSystemOperations method unzip.

/**
     * Unzip file to local or remote machine, if the machine is UNIX-like it will preserve the permissions
     *
     * @param zipFilePath the zip file path
     * @param outputDirPath output directory
     * @param isTempDirectory whether the directory is temporary or not. Temporary means that
     *  it will be automatically deleted only if the virtual machine terminates normally.
     * @throws Exception
     */
@Override
public void unzip(String zipFilePath, String outputDirPath) throws FileSystemOperationException {
    ZipArchiveEntry zipEntry = null;
    File outputDir = new File(outputDirPath);
    //check if the dir is created
    outputDir.mkdirs();
    try (ZipFile zipFile = new ZipFile(zipFilePath)) {
        Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
        int unixPermissions = 0;
        while (entries.hasMoreElements()) {
            zipEntry = entries.nextElement();
            File entryDestination = new File(outputDirPath, zipEntry.getName());
            unixPermissions = zipEntry.getUnixMode();
            if (zipEntry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = null;
                OutputStream out = null;
                in = zipFile.getInputStream(zipEntry);
                out = new BufferedOutputStream(new FileOutputStream(entryDestination));
                IoUtils.copyStream(in, out);
            }
            if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
                //check if the OS is UNIX
                // set file/dir permissions, after it is created
                Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(octalToDecimal(unixPermissions)));
            }
        }
    } catch (Exception e) {
        String errorMsg = "Unable to unzip " + ((zipEntry != null) ? zipEntry.getName() + " from " : "") + zipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
        throw new FileSystemOperationException(errorMsg, e);
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 13 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project tika by apache.

the class ZipContainerDetector method detectZipFormat.

private static MediaType detectZipFormat(TikaInputStream tis) {
    try {
        //try opc first because opening a package
        //will not necessarily throw an exception for
        //truncated files.
        MediaType type = detectOPCBased(tis);
        if (type != null) {
            return type;
        }
        // TODO: hasFile()?
        ZipFile zip = new ZipFile(tis.getFile());
        try {
            type = detectOpenDocument(zip);
            if (type == null) {
                type = detectIWork13(zip);
            }
            if (type == null) {
                type = detectIWork(zip);
            }
            if (type == null) {
                type = detectJar(zip);
            }
            if (type == null) {
                type = detectKmz(zip);
            }
            if (type == null) {
                type = detectIpa(zip);
            }
            if (type != null) {
                return type;
            }
        } finally {
            // tis.setOpenContainer(zip);
            try {
                zip.close();
            } catch (IOException e) {
            // ignore
            }
        }
    } catch (IOException e) {
    // ignore
    }
    // Fallback: it's still a zip file, we just don't know what kind of one
    return MediaType.APPLICATION_ZIP;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) MediaType(org.apache.tika.mime.MediaType) IOException(java.io.IOException)

Example 14 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project tika by apache.

the class CXFTestBase method readArchiveText.

protected String readArchiveText(InputStream inputStream) throws IOException {
    Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
    ZipFile zip = new ZipFile(tempFile.toFile());
    zip.getEntry(UnpackerResource.TEXT_FILENAME);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(zip.getInputStream(zip.getEntry(UnpackerResource.TEXT_FILENAME)), bos);
    zip.close();
    Files.delete(tempFile);
    return bos.toString(UTF_8.name());
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project stanbol by apache.

the class ConfigUtils method copyCore.

/**
     * Copy the configuration of an core.
     * 
     * @param clazzInArchive
     *            This class is used to identify the archive containing the default configuration. Parsing
     *            <code>null</code> causes this class to be used and therefore initialises the default core
     *            configuration contained by the SolrYard bundle.
     * @param coreDir
     *            the target directory for the core
     * @param coreName
     *            the core name or <code>null</code> to directly load the configuration as present under
     *            {@value #CONFIG_DIR} in the bundle. This property can be used if a bundle needs to provide
     *            multiple core configurations
     * @param override
     *            if files in the target directory should be overridden
     * @throws IOException
     *             On any IO error while coping the configuration
     * @throws IllegalStateException
     *             If the parsed coreDir does exist but is not a directory.
     * @throws IllegalArgumentException
     *             if <code>null</code> is parsed as coreDir or if the parsed bundle does not contain the
     *             required information to set up an configuration or the parsed coreName is empty.
     */
public static void copyCore(Class<?> clazzInArchive, File coreDir, String coreName, boolean override) throws IOException, IllegalArgumentException, IllegalStateException {
    if (coreDir == null) {
        throw new IllegalArgumentException("The parsed core directory MUST NOT be NULL!");
    }
    if (coreDir.exists() && !coreDir.isDirectory()) {
        throw new IllegalStateException("The parsed core directory " + coreDir.getAbsolutePath() + " extists but is not a directory!");
    }
    if (coreName != null && coreName.isEmpty()) {
        throw new IllegalArgumentException("The parsed core name MUST NOT be empty (However NULL is supported)!");
    }
    String context = CORE_CONFIG_DIR + (coreName != null ? '/' + coreName : "");
    File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
    if (sourceRoot.isFile()) {
        ZipFile archive = new ZipFile(sourceRoot);
        log.info(String.format("Copy core %s config from jar-file %s to %s (override=%s)", (coreName == null ? "" : coreName), sourceRoot.getName(), coreDir.getAbsolutePath(), override));
        try {
            for (@SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = archive.getEntries(); entries.hasMoreElements(); ) {
                ZipArchiveEntry entry = entries.nextElement();
                if (entry.getName().startsWith(context)) {
                    copyResource(coreDir, archive, entry, context, override);
                }
            }
        } finally {
            // regardless what happens we need to close the archive!
            ZipFile.closeQuietly(archive);
        }
    } else {
        // load from file
        File source = new File(sourceRoot, context);
        if (source.exists() && source.isDirectory()) {
            FileUtils.copyDirectory(source, coreDir);
        } else {
            throw new FileNotFoundException("The SolrIndex default config was not found in directory " + source.getAbsolutePath());
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileNotFoundException(java.io.FileNotFoundException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)27 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)21 IOException (java.io.IOException)10 File (java.io.File)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 ZipInputStream (java.util.zip.ZipInputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 BufferedInputStream (java.io.BufferedInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 OutputStream (java.io.OutputStream)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1