Search in sources :

Example 36 with ZipEntry

use of java.util.zip.ZipEntry 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 37 with ZipEntry

use of java.util.zip.ZipEntry 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 38 with ZipEntry

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

the class ZipManager method zipBackupJsonFile.

private void zipBackupJsonFile(String backupJsonFilePath, ZipOutputStream out) throws IOException {
    byte[] data = new byte[BUFFER_SIZE];
    FileInputStream fi = new FileInputStream(backupJsonFilePath);
    BufferedInputStream jsonFileOrigin = new BufferedInputStream(fi, BUFFER_SIZE);
    ZipEntry entry = new ZipEntry(BackupManager.BACKUP_JSON_FILE_NAME);
    out.putNextEntry(entry);
    int count;
    while ((count = jsonFileOrigin.read(data, 0, BUFFER_SIZE)) != -1) {
        out.write(data, 0, count);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

Example 39 with ZipEntry

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

the class AgentUtil method getJarSignature.

public static String getJarSignature(String packagePath) throws IOException {
    Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");
    ZipFile packageZip = null;
    try {
        packageZip = new ZipFile(packagePath);
        // For each file in the zip.
        for (ZipEntry entry : Collections.list(packageZip.entries())) {
            // Ignore non-signature files.
            if (!signatureFilePattern.matcher(entry.getName()).matches()) {
                continue;
            }
            BufferedReader sigContents = null;
            try {
                sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
                // For each line in the signature file.
                while (true) {
                    String line = sigContents.readLine();
                    if (line == null || line.equals("")) {
                        throw new IllegalArgumentException("Failed to find manifest digest in " + entry.getName());
                    }
                    String prefix = "SHA1-Digest-Manifest: ";
                    if (line.startsWith(prefix)) {
                        return line.substring(prefix.length());
                    }
                }
            } finally {
                if (sigContents != null) {
                    sigContents.close();
                }
            }
        }
    } finally {
        if (packageZip != null) {
            packageZip.close();
        }
    }
    throw new IllegalArgumentException("Failed to find signature file.");
}
Also used : Pattern(java.util.regex.Pattern) ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) BufferedReader(java.io.BufferedReader)

Example 40 with ZipEntry

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

the class DalvikAwareOutputStreamHelper method putEntry.

@Override
public void putEntry(FileLike fileLike) throws IOException {
    String name = fileLike.getRelativePath();
    // proguard seems to handle merging multiple -injars into a single -outjar.
    if (!containsEntry(fileLike)) {
        entryNames.add(name);
        outStream.putNextEntry(new ZipEntry(name));
        try (InputStream in = fileLike.getInput()) {
            ByteStreams.copy(in, outStream);
        }
        // Make sure FileLike#getSize didn't lie (or we forgot to call canPutEntry).
        DalvikStatsTool.Stats stats = dalvikStatsCache.getStats(fileLike);
        Preconditions.checkState(!isEntryTooBig(fileLike), "Putting entry %s (%s) exceeded maximum size of %s", name, stats.estimatedLinearAllocSize, linearAllocLimit);
        currentLinearAllocSize += stats.estimatedLinearAllocSize;
        currentMethodReferences.addAll(stats.methodReferences);
        currentFieldReferences.addAll(stats.fieldReferences);
        String report = String.format("%d %d %s\n", stats.estimatedLinearAllocSize, stats.methodReferences.size(), name);
        reportFileWriter.append(report);
    }
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56