Search in sources :

Example 6 with ZipInputStream

use of java.util.zip.ZipInputStream in project che by eclipse.

the class ZipArchiverTest method assertThatZipArchiveContainsAllEntries.

private void assertThatZipArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception {
    try (ZipInputStream zip = new ZipInputStream(in)) {
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            String name = zipEntry.getName();
            assertTrue(String.format("Unexpected entry %s in zip", name), entries.containsKey(name));
            if (!zipEntry.isDirectory()) {
                String content = new String(ByteStreams.toByteArray(zip));
                assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
            }
            entries.remove(name);
            zip.closeEntry();
        }
    }
    assertTrue(String.format("Expected but were not found in zip %s", entries), entries.isEmpty());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 7 with ZipInputStream

use of java.util.zip.ZipInputStream in project che by eclipse.

the class ZipArchiver method extract.

@Override
public void extract(InputStream zipInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
    try (ZipInputStream zip = new ZipInputStream(ZipContent.of(zipInput).getContent())) {
        InputStream notClosableInputStream = new NotClosableInputStream(zip);
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            VirtualFile extractFolder = folder;
            Path relativePath = Path.of(zipEntry.getName());
            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }
                relativePath = relativePath.subPath(stripNumber);
            }
            if (zipEntry.isDirectory()) {
                if (!extractFolder.hasChild(relativePath)) {
                    extractFolder.createFolder(relativePath.toString());
                }
                continue;
            }
            if (relativePath.length() > 1) {
                Path neededParentPath = relativePath.getParent();
                VirtualFile neededParent = extractFolder.getChild(neededParentPath);
                if (neededParent == null) {
                    neededParent = extractFolder.createFolder(neededParentPath.toString());
                }
                extractFolder = neededParent;
            }
            String fileName = relativePath.getName();
            VirtualFile file = extractFolder.getChild(Path.of(fileName));
            if (file == null) {
                extractFolder.createFile(fileName, notClosableInputStream);
            } else {
                if (overwrite) {
                    file.updateContent(notClosableInputStream);
                } else {
                    throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
                }
            }
            zip.closeEntry();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ConflictException(org.eclipse.che.api.core.ConflictException) ZipInputStream(java.util.zip.ZipInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream)

Example 8 with ZipInputStream

use of java.util.zip.ZipInputStream in project elasticsearch by elastic.

the class FileTestUtils method unzip.

/**
     * Unzip a zip file to a destination directory.  If the zip file does not exist, an IOException is thrown.
     * If the destination directory does not exist, it will be created.
     *
     * @param zip      zip file to unzip
     * @param destDir  directory to unzip the file to
     * @param prefixToRemove  the (optional) prefix in the zip file path to remove when writing to the destination directory
     * @throws IOException if zip file does not exist, or there was an error reading from the zip file or
     *                     writing to the destination directory
     */
public static void unzip(final Path zip, final Path destDir, @Nullable final String prefixToRemove) throws IOException {
    if (Files.notExists(zip)) {
        throw new IOException("[" + zip + "] zip file must exist");
    }
    Files.createDirectories(destDir);
    try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            final String entryPath;
            if (prefixToRemove != null) {
                if (entry.getName().startsWith(prefixToRemove)) {
                    entryPath = entry.getName().substring(prefixToRemove.length());
                } else {
                    throw new IOException("prefix not found: " + prefixToRemove);
                }
            } else {
                entryPath = entry.getName();
            }
            final Path path = Paths.get(destDir.toString(), entryPath);
            if (entry.isDirectory()) {
                Files.createDirectories(path);
            } else {
                Files.copy(zipInput, path);
            }
            zipInput.closeEntry();
        }
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 9 with ZipInputStream

use of java.util.zip.ZipInputStream in project MyDiary by erttyy8821.

the class ZipManager method unzip.

public void unzip(String backupZieFilePath, String location) throws IOException {
    int size;
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        if (!location.endsWith("/")) {
            location += "/";
        }
        File f = new File(location);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(backupZieFilePath), BUFFER_SIZE));
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + ze.getName();
                File unzipFile = new File(path);
                if (ze.isDirectory()) {
                    if (!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    // check for and create parent directories if they don't exist
                    File parentDir = unzipFile.getParentFile();
                    if (null != parentDir) {
                        if (!parentDir.isDirectory()) {
                            parentDir.mkdirs();
                        }
                    }
                    // unzip the file
                    FileOutputStream out = new FileOutputStream(unzipFile, false);
                    BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
                    try {
                        while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) {
                            fout.write(buffer, 0, size);
                        }
                        zin.closeEntry();
                    } finally {
                        fout.flush();
                        fout.close();
                    }
                }
            }
        } finally {
            zin.close();
        }
    } catch (Exception e) {
        Log.e(TAG, "Unzip exception", e);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 10 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class RepackZipEntriesStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) {
    Path inputFile = filesystem.getPathForRelativePath(inputPath);
    Path outputFile = filesystem.getPathForRelativePath(outputPath);
    try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(inputFile)));
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(outputFile)) {
        for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
            CustomZipEntry customEntry = new CustomZipEntry(entry);
            if (entries.contains(customEntry.getName())) {
                customEntry.setCompressionLevel(compressionLevel.getValue());
            }
            InputStream toUse;
            // If we're using STORED files, we must pre-calculate the CRC.
            if (customEntry.getMethod() == ZipEntry.STORED) {
                try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                    ByteStreams.copy(in, bos);
                    byte[] bytes = bos.toByteArray();
                    customEntry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
                    customEntry.setSize(bytes.length);
                    customEntry.setCompressedSize(bytes.length);
                    toUse = new ByteArrayInputStream(bytes);
                }
            } else {
                toUse = in;
            }
            out.putNextEntry(customEntry);
            ByteStreams.copy(toUse, out);
            out.closeEntry();
        }
        return StepExecutionResult.SUCCESS;
    } catch (IOException e) {
        context.logError(e, "Unable to repack zip");
        return StepExecutionResult.ERROR;
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)354 ZipEntry (java.util.zip.ZipEntry)259 FileInputStream (java.io.FileInputStream)120 File (java.io.File)115 IOException (java.io.IOException)103 ByteArrayInputStream (java.io.ByteArrayInputStream)63 FileOutputStream (java.io.FileOutputStream)63 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 Test (org.junit.Test)56 InputStream (java.io.InputStream)53 BufferedInputStream (java.io.BufferedInputStream)47 ZipOutputStream (java.util.zip.ZipOutputStream)31 FileNotFoundException (java.io.FileNotFoundException)21 Path (java.nio.file.Path)20 HashMap (java.util.HashMap)19 BufferedOutputStream (java.io.BufferedOutputStream)18 ArrayList (java.util.ArrayList)18 ZipFile (java.util.zip.ZipFile)18 OutputStream (java.io.OutputStream)17 GZIPInputStream (java.util.zip.GZIPInputStream)14