use of java.util.zip.ZipOutputStream in project SmartAndroidSource by jaychou2012.
the class ZipUtil method packEntry.
/* Compressing single entries to ZIP files. */
/**
* Compresses the given file into a ZIP file with single entry.
*
* @param file
* file to be compressed.
* @return ZIP file created.
*/
public static byte[] packEntry(File file) {
// log.trace("Compressing '{}' into a ZIP file with single entry.",
// file);
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
ZipOutputStream out = new ZipOutputStream(result);
ZipEntry entry = new ZipEntry(file.getName());
entry.setTime(file.lastModified());
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
ZipEntryUtil.addEntry(entry, in, out);
} finally {
IOUtils.closeQuietly(in);
}
out.close();
} catch (IOException e) {
throw ZipExceptionUtil.rethrow(e);
}
return result.toByteArray();
}
use of java.util.zip.ZipOutputStream in project SmartAndroidSource by jaychou2012.
the class ZipUtil method replaceEntries.
/**
* Copies an existing ZIP file and replaces the given entries in it.
*
* @param zip
* an existing ZIP file (only read).
* @param entries
* new ZIP entries to be replaced with.
* @param destZip
* new ZIP file created.
* @return <code>true</code> if at least one entry was replaced.
*/
public static boolean replaceEntries(File zip, ZipEntrySource[] entries, File destZip) {
// if (log.isDebugEnabled()) {
// log.debug("Copying '" + zip + "' to '" + destZip
// + "' and replacing entries " + Arrays.asList(entries) + ".");
// }
final Map<String, ZipEntrySource> entryByPath = entriesByPath(entries);
final int entryCount = entryByPath.size();
try {
final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZip)));
try {
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());
// }
}
});
} finally {
IOUtils.closeQuietly(out);
}
} catch (IOException e) {
ZipExceptionUtil.rethrow(e);
}
return entryByPath.size() < entryCount;
}
use of java.util.zip.ZipOutputStream in project Lazy by l123456789jy.
the class ZipUtil method zipFiles.
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param zipListener zipListener
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, ZipListener zipListener) {
ZipOutputStream zipout = null;
try {
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
if (stopZipFlag) {
break;
}
zipFile(resFile, zipout, "", zipListener);
}
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of java.util.zip.ZipOutputStream in project neo4j by neo4j.
the class ReadablesTest method compressWithZip.
// TODO test for failing reading a ZIP archive with multiple files in
private File compressWithZip(String text, String... otherEntries) throws IOException {
File file = directory.file("compressed");
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) {
for (String otherEntry : otherEntries) {
out.putNextEntry(new ZipEntry(otherEntry));
}
out.putNextEntry(new ZipEntry("file"));
out.write(text.getBytes());
}
return file;
}
use of java.util.zip.ZipOutputStream in project neo4j by neo4j.
the class PartitionedIndexStorage method cleanupFolder.
private void cleanupFolder(File folder, boolean archiveFailed) throws IOException {
List<File> partitionFolders = listFolders(folder);
if (!partitionFolders.isEmpty()) {
try (ZipOutputStream zip = archiveFile(folder, archiveFailed)) {
byte[] buffer = null;
if (zip != null) {
buffer = new byte[4 * 1024];
}
for (File partitionFolder : partitionFolders) {
cleanupLuceneDirectory(partitionFolder, zip, buffer);
}
}
}
fileSystem.deleteRecursively(folder);
}
Aggregations