use of java.util.zip.ZipOutputStream in project tinker by Tencent.
the class FileUtil method differZip.
/**
* differ zip
*
* @param differentOutputFullFilename
* @param oldZipFullFilename
* @param newZipFullFilename
*/
public static void differZip(String differentOutputFullFilename, String oldZipFullFilename, String newZipFullFilename) {
Map<String, String> map = getZipEntryHashMap(oldZipFullFilename);
ZipFile newZipFile = null;
ZipOutputStream zipOutputStream = null;
try {
newZipFile = new ZipFile(newZipFullFilename);
Enumeration<? extends ZipEntry> entries = newZipFile.entries();
FileUtil.createFile(differentOutputFullFilename);
zipOutputStream = new ZipOutputStream(new FileOutputStream(differentOutputFullFilename));
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (!zipEntry.isDirectory()) {
String zipEntryName = zipEntry.getName();
String oldZipEntryHash = map.get(zipEntryName);
String newZipEntryHash = zipEntry.getCrc() + Constant.Symbol.DOT + zipEntry.getSize();
// is a modified zip entry
if (oldZipEntryHash == null || (!newZipEntryHash.equals(oldZipEntryHash))) {
System.out.println(String.format("found modified entry, key=%s(%s/%s)", new Object[] { zipEntryName, oldZipEntryHash, newZipEntryHash }));
addZipEntry(zipOutputStream, zipEntry, newZipFile.getInputStream(zipEntry));
}
}
}
} catch (Exception e) {
throw new FileUtilException(e);
} finally {
if (newZipFile != null) {
try {
newZipFile.close();
} catch (IOException e) {
throw new FileUtilException(e);
}
}
if (zipOutputStream != null) {
try {
zipOutputStream.finish();
} catch (IOException e) {
throw new FileUtilException(e);
}
}
}
}
use of java.util.zip.ZipOutputStream in project tinker by Tencent.
the class FileUtil method mergeZip.
/**
* merge zip file
*
* @param zipOutputFullFilename
* @param mergeZipFullFilenameList
*/
public static void mergeZip(String zipOutputFullFilename, List<String> mergeZipFullFilenameList) {
FileUtil.createFile(zipOutputFullFilename);
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutputFullFilename));
if (mergeZipFullFilenameList != null) {
for (String zipFullFilename : mergeZipFullFilenameList) {
if (isExist(zipFullFilename)) {
ZipFile zipFile = new ZipFile(zipFullFilename);
Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = enumeration.nextElement();
InputStream inputStream = zipFile.getInputStream(zipEntry);
addZipEntry(zipOutputStream, zipEntry, inputStream);
}
zipFile.close();
}
}
}
} catch (Exception e) {
throw new FileUtilException(e);
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (Exception e) {
throw new FileUtilException(e);
}
}
}
use of java.util.zip.ZipOutputStream in project MinecraftForge by MinecraftForge.
the class AccessTransformer method processJar.
private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
ZipInputStream inJar = null;
ZipOutputStream outJar = null;
try {
try {
inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open input file: " + e.getMessage());
}
try {
outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open output file: " + e.getMessage());
}
ZipEntry entry;
while ((entry = inJar.getNextEntry()) != null) {
if (entry.isDirectory()) {
outJar.putNextEntry(entry);
continue;
}
byte[] data = new byte[4096];
ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
int len;
do {
len = inJar.read(data);
if (len > 0) {
entryBuffer.write(data, 0, len);
}
} while (len != -1);
byte[] entryData = entryBuffer.toByteArray();
String entryName = entry.getName();
if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
ClassNode cls = new ClassNode();
ClassReader rdr = new ClassReader(entryData);
rdr.accept(cls, 0);
String name = cls.name.replace('/', '.').replace('\\', '.');
for (AccessTransformer trans : transformers) {
entryData = trans.transform(name, name, entryData);
}
}
ZipEntry newEntry = new ZipEntry(entryName);
outJar.putNextEntry(newEntry);
outJar.write(entryData);
}
} finally {
IOUtils.closeQuietly(outJar);
IOUtils.closeQuietly(inJar);
}
}
use of java.util.zip.ZipOutputStream in project MinecraftForge by MinecraftForge.
the class MarkerTransformer method processJar.
private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
ZipInputStream inJar = null;
ZipOutputStream outJar = null;
try {
try {
inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open input file: " + e.getMessage());
}
try {
outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open output file: " + e.getMessage());
}
ZipEntry entry;
while ((entry = inJar.getNextEntry()) != null) {
if (entry.isDirectory()) {
outJar.putNextEntry(entry);
continue;
}
byte[] data = new byte[4096];
ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
int len;
do {
len = inJar.read(data);
if (len > 0) {
entryBuffer.write(data, 0, len);
}
} while (len != -1);
byte[] entryData = entryBuffer.toByteArray();
String entryName = entry.getName();
if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
ClassNode cls = new ClassNode();
ClassReader rdr = new ClassReader(entryData);
rdr.accept(cls, 0);
String name = cls.name.replace('/', '.').replace('\\', '.');
for (MarkerTransformer trans : transformers) {
entryData = trans.transform(name, name, entryData);
}
}
ZipEntry newEntry = new ZipEntry(entryName);
outJar.putNextEntry(newEntry);
outJar.write(entryData);
}
} finally {
IOUtils.closeQuietly(outJar);
IOUtils.closeQuietly(inJar);
}
}
use of java.util.zip.ZipOutputStream in project atlas by alibaba.
the class ZipUtils method addFileAndDirectoryToZip.
public static void addFileAndDirectoryToZip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception {
if (output.isDirectory()) {
throw new IOException("This is a directory!");
}
if (!output.getParentFile().exists()) {
output.getParentFile().mkdirs();
}
if (!output.exists()) {
output.createNewFile();
}
List fileList = getSubFiles(srcDir);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
if (zipEntryMethodMap != null) {
ZipEntry originEntry = zipEntryMethodMap.get(f.getAbsolutePath());
if (originEntry != null) {
ze.setCompressedSize(originEntry.getCompressedSize());
ze.setCrc(originEntry.getCrc());
ze.setMethod(originEntry.getMethod());
}
}
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
}
Aggregations