[updater] minor optimizations

This commit is contained in:
Roman Shevchenko
2016-09-15 20:35:09 +03:00
parent 7eb97d5d86
commit 0e8c9d4c3c
2 changed files with 38 additions and 52 deletions

View File

@@ -171,9 +171,7 @@ public class UpdateZipAction extends BaseUpdateAction {
@Override
protected void doApply(final ZipFile patchFile, File backupDir, File toFile) throws IOException {
File temp = Utils.createTempFile();
try (ZipOutputWrapper out = new ZipOutputWrapper(new FileOutputStream(temp))) {
out.setCompressionLevel(0);
try (ZipOutputWrapper out = new ZipOutputWrapper(new FileOutputStream(temp), 0)) {
processZipFile(getSource(backupDir), new Processor() {
@Override
public void process(ZipEntry entry, InputStream in) throws IOException {

View File

@@ -16,75 +16,70 @@
package com.intellij.updater;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputWrapper implements AutoCloseable {
private final ZipOutputStream myOut;
private final Set<String> myDirs = new HashSet<>();
private boolean isCompressed = true;
private final boolean myCompressed;
private final Set<String> myDirs = new TreeSet<>();
public ZipOutputWrapper(OutputStream stream) {
myOut = new ZipOutputStream(new BufferedOutputStream(stream));
myCompressed = true;
}
public void setCompressionLevel(int level) {
myOut.setLevel(level);
if (level == 0) {
myOut.setMethod(ZipEntry.STORED);
isCompressed = false;
}
public ZipOutputWrapper(OutputStream stream, int compressionLevel) {
myOut = new ZipOutputStream(new BufferedOutputStream(stream));
myOut.setLevel(compressionLevel);
myCompressed = compressionLevel > 0;
}
public OutputStream zipStream(final String entryPath) throws IOException {
final ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
return new BufferedOutputStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
tempOut.write(b);
}
public OutputStream zipStream(String entryPath) throws IOException {
return new OptByteArrayOutputStream() {
@Override
public void close() throws IOException {
super.close();
tempOut.close();
zipBytes(entryPath, tempOut);
zipBytes(entryPath, this);
}
});
};
}
public void zipEntry(ZipEntry entry, InputStream from) throws IOException {
if (entry.isDirectory()) {
addDirs(entry.getName(), true);
return;
}
zipEntry(entry.getName(), from);
else {
zipEntry(entry.getName(), from);
}
}
public void zipEntry(String entryPath, InputStream from) throws IOException {
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
try {
Utils.copyStream(from, tempOut);
}
finally {
tempOut.close();
}
OptByteArrayOutputStream tempOut = new OptByteArrayOutputStream();
Utils.copyStream(from, tempOut);
zipBytes(entryPath, tempOut);
}
public void zipBytes(String entryPath, ByteArrayOutputStream byteOut) throws IOException {
public void zipFile(String entryPath, File file) throws IOException {
if (file.isDirectory()) {
throw new IllegalArgumentException("Doesn't make sense");
}
try (InputStream from = new BufferedInputStream(new FileInputStream(file))) {
zipEntry(new ZipEntry(entryPath), from);
}
}
private void zipBytes(String entryPath, OptByteArrayOutputStream byteOut) throws IOException {
addDirs(entryPath, false);
ZipEntry entry = new ZipEntry(entryPath);
if (!isCompressed) {
if (!myCompressed) {
entry.setSize(byteOut.size());
CRC32 crc = new CRC32();
crc.update(byteOut.toByteArray());
byteOut.updateChecksum(crc);
entry.setCrc(crc.getValue());
}
@@ -93,25 +88,12 @@ public class ZipOutputWrapper implements AutoCloseable {
myOut.closeEntry();
}
public void zipFile(String entryPath, File file) throws IOException {
if (file.isDirectory()) {
addDirs(entryPath, true);
return;
}
try (InputStream from = new BufferedInputStream(new FileInputStream(file))) {
zipEntry(new ZipEntry(entryPath), from);
}
}
private void addDirs(String relPath, boolean isDir) {
List<String> temp = new ArrayList<>();
if (isDir && !relPath.endsWith("/")) relPath += "/";
int index = 0;
while ((index = relPath.indexOf('/', index + 1)) != -1) {
temp.add(relPath.substring(0, index));
myDirs.add(relPath.substring(0, index));
}
myDirs.addAll(temp);
}
public void finish() throws IOException {
@@ -130,4 +112,10 @@ public class ZipOutputWrapper implements AutoCloseable {
public void close() throws IOException {
myOut.close();
}
private static class OptByteArrayOutputStream extends ByteArrayOutputStream {
public void updateChecksum(Checksum cs) {
cs.update(buf, 0, count);
}
}
}