Search in sources :

Example 36 with ZipOutputStream

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

the class ZipUtil method transformEntries.

/**
	 * Copies an existing ZIP file and transforms the given entries in it.
	 * 
	 * @param is
	 *            a ZIP input stream.
	 * @param entries
	 *            ZIP entry transformers.
	 * @param os
	 *            a ZIP output stream.
	 * @return <code>true</code> if at least one entry was replaced.
	 */
public static boolean transformEntries(InputStream is, ZipEntryTransformerEntry[] entries, OutputStream os) {
    try {
        ZipOutputStream out = new ZipOutputStream(os);
        TransformerZipEntryCallback action = new TransformerZipEntryCallback(Arrays.asList(entries), out);
        iterate(is, action);
        // Finishes writing the contents of the ZIP output stream without
        // closing
        // the underlying stream.
        out.finish();
        return action.found();
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException)

Example 37 with ZipOutputStream

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

the class ZipUtil method transformEntries.

/**
	 * Copies an existing ZIP file and transforms the given entries in it.
	 * 
	 * @param zip
	 *            an existing ZIP file (only read).
	 * @param entries
	 *            ZIP entry transformers.
	 * @param destZip
	 *            new ZIP file created.
	 * @return <code>true</code> if at least one entry was replaced.
	 */
public static boolean transformEntries(File zip, ZipEntryTransformerEntry[] entries, File destZip) {
    try {
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZip)));
        try {
            TransformerZipEntryCallback action = new TransformerZipEntryCallback(Arrays.asList(entries), out);
            iterate(zip, action);
            return action.found();
        } finally {
            IOUtils.closeQuietly(out);
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 38 with ZipOutputStream

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

the class ZipUtil method addEntries.

/**
	 * Copies an existing ZIP file and appends it with new entries.
	 * 
	 * @param zip
	 *            an existing ZIP file (only read).
	 * @param entries
	 *            new ZIP entries appended.
	 * @param destZip
	 *            new ZIP file created.
	 */
public static void addEntries(File zip, ZipEntrySource[] entries, File destZip) {
    // if (log.isDebugEnabled()) {
    // log.debug("Copying '" + zip + "' to '" + destZip + "' and adding "
    // + Arrays.asList(entries) + ".");
    // }
    ZipOutputStream out = null;
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZip)));
        copyEntries(zip, out);
        for (int i = 0; i < entries.length; i++) {
            addEntry(entries[i], out);
        }
    } catch (IOException e) {
        ZipExceptionUtil.rethrow(e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 39 with ZipOutputStream

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

the class Zips method process.

/**
	 * Iterates through source Zip entries removing or changing them according
	 * to set parameters.
	 */
public void process() {
    if (src == null && dest == null) {
        throw new IllegalArgumentException("Source and destination shouldn't be null together");
    }
    File destinationFile = null;
    try {
        destinationFile = getDestinationFile();
        ZipOutputStream out = null;
        ZipEntryOrInfoAdapter zipEntryAdapter = null;
        if (destinationFile.isFile()) {
            out = ZipFileUtil.createZipOutputStream(new BufferedOutputStream(new FileOutputStream(destinationFile)), charset);
            zipEntryAdapter = new ZipEntryOrInfoAdapter(new CopyingCallback(transformers, out, preserveTimestamps), null);
        } else {
            // directory
            zipEntryAdapter = new ZipEntryOrInfoAdapter(new UnpackingCallback(transformers, destinationFile), null);
        }
        try {
            processAllEntries(zipEntryAdapter);
        } finally {
            IOUtils.closeQuietly(out);
        }
        handleInPlaceActions(destinationFile);
    } catch (IOException e) {
        ZipExceptionUtil.rethrow(e);
    } finally {
        if (isInPlace()) {
            // destinationZip is a temporary file
            FileUtils.deleteQuietly(destinationFile);
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 40 with ZipOutputStream

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

the class ZipUtil method addOrReplaceEntries.

/**
	 * Copies an existing ZIP file and adds/replaces the given entries in it.
	 * 
	 * @param zip
	 *            an existing ZIP file (only read).
	 * @param entries
	 *            ZIP entries to be replaced or added.
	 * @param destZip
	 *            new ZIP file created.
	 */
public static void addOrReplaceEntries(File zip, ZipEntrySource[] entries, File destZip) {
    // if (log.isDebugEnabled()) {
    // log.debug("Copying '" + zip + "' to '" + destZip
    // + "' and adding/replacing entries "
    // + Arrays.asList(entries) + ".");
    // }
    final Map<String, ZipEntrySource> entryByPath = entriesByPath(entries);
    try {
        final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZip)));
        try {
            // Copy and replace entries
            final Set<String> names = new HashSet<String>();
            iterate(zip, new ZipEntryCallback() {

                public void process(InputStream in, ZipEntry zipEntry) throws IOException {
                    if (names.add(zipEntry.getName())) {
                        ZipEntrySource entry = (ZipEntrySource) entryByPath.remove(zipEntry.getName());
                        if (entry != null) {
                            addEntry(entry, out);
                        } else {
                            ZipEntryUtil.copyEntry(zipEntry, in, out);
                        }
                    }
                // else if (log.isDebugEnabled()) {
                // log.debug("Duplicate entry: {}", zipEntry.getName());
                // }
                }
            });
            // Add new entries
            for (ZipEntrySource zipEntrySource : entryByPath.values()) {
                addEntry(zipEntrySource, out);
            }
        } finally {
            IOUtils.closeQuietly(out);
        }
    } catch (IOException e) {
        ZipExceptionUtil.rethrow(e);
    }
}
Also used : 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) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)463 ZipEntry (java.util.zip.ZipEntry)308 FileOutputStream (java.io.FileOutputStream)216 File (java.io.File)181 IOException (java.io.IOException)118 ZipFile (java.util.zip.ZipFile)104 BufferedOutputStream (java.io.BufferedOutputStream)90 ByteArrayOutputStream (java.io.ByteArrayOutputStream)83 FileInputStream (java.io.FileInputStream)82 Test (org.junit.Test)68 InputStream (java.io.InputStream)61 ZipInputStream (java.util.zip.ZipInputStream)44 ByteArrayInputStream (java.io.ByteArrayInputStream)39 BufferedInputStream (java.io.BufferedInputStream)28 OutputStream (java.io.OutputStream)26 Path (java.nio.file.Path)22 ArrayList (java.util.ArrayList)19 FileNotFoundException (java.io.FileNotFoundException)18 HashMap (java.util.HashMap)17 CRC32 (java.util.zip.CRC32)13