Search in sources :

Example 26 with ZipInputStream

use of java.util.zip.ZipInputStream in project atlas by alibaba.

the class JarSplitUtils method splitZip.

public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException {
    if (outputFile.exists()) {
        FileUtils.deleteQuietly(outputFile);
    }
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    FileOutputStream fos = new FileOutputStream(outputFile);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);
    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);
            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 27 with ZipInputStream

use of java.util.zip.ZipInputStream in project atlas by alibaba.

the class ZipUtils method addFileToZipFile.

/**
     * 增加文件到zip文件
     *
     * @param zipFile
     * @param file
     * @param destPath  要放的路径
     * @param overwrite 是否覆盖
     * @throws IOException
     */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath, boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (ZipEntry.STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

Example 28 with ZipInputStream

use of java.util.zip.ZipInputStream in project hudson-2.x by hudson.

the class FilePath method unzip.

private void unzip(File dir, InputStream in) throws IOException {
    // without absolutization, getParentFile below seems to fail
    dir = dir.getAbsoluteFile();
    ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
    java.util.zip.ZipEntry e;
    try {
        while ((e = zip.getNextEntry()) != null) {
            File f = new File(dir, e.getName());
            if (e.isDirectory()) {
                f.mkdirs();
            } else {
                File p = f.getParentFile();
                if (p != null)
                    p.mkdirs();
                IOUtils.copy(zip, f);
                f.setLastModified(e.getTime());
                zip.closeEntry();
            }
        }
    } finally {
        zip.close();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File)

Example 29 with ZipInputStream

use of java.util.zip.ZipInputStream in project SmartAndroidSource by jaychou2012.

the class ZipUtil method iterate.

/**
	 * Reads the given ZIP stream and executes the given action for each entry.
	 * <p>
	 * For each entry the corresponding input stream is also passed to the
	 * action. If you want to stop the loop then throw a ZipBreakException.
	 * 
	 * @param is
	 *            input ZIP stream (it will not be closed automatically).
	 * @param action
	 *            action to be called for each entry.
	 * 
	 * @see ZipEntryCallback
	 * @see #iterate(File, ZipEntryCallback)
	 */
public static void iterate(InputStream is, ZipEntryCallback action, Charset charset) {
    try {
        ZipInputStream in = null;
        if (charset == null) {
            in = new ZipInputStream(new BufferedInputStream(is));
        } else {
            in = ZipFileUtil.createZipInputStream(is, charset);
        }
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            try {
                action.process(in, entry);
            } catch (IOException ze) {
                throw new ZipException("Failed to process zip entry '" + entry.getName() + " with action " + action, ze);
            } catch (ZipBreakException ex) {
                break;
            }
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 30 with ZipInputStream

use of java.util.zip.ZipInputStream in project android-sqlite-asset-helper by jgilfelt.

the class Utils method getFileFromZip.

public static ZipInputStream getFileFromZip(InputStream zipFileStream) throws IOException {
    ZipInputStream zis = new ZipInputStream(zipFileStream);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        Log.w(TAG, "extracting file: '" + ze.getName() + "'...");
        return zis;
    }
    return null;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

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