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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations