mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[platform] makes ZIP decompressor fail on corrupted files
This commit is contained in:
@@ -11,8 +11,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public abstract class Decompressor<Stream> {
|
||||
public static class Tar extends Decompressor<TarArchiveInputStream> {
|
||||
@@ -57,37 +58,42 @@ public abstract class Decompressor<Stream> {
|
||||
//</editor-fold>
|
||||
}
|
||||
|
||||
public static class Zip extends Decompressor<ZipInputStream> {
|
||||
public static class Zip extends Decompressor<ZipFile> {
|
||||
public Zip(@NotNull File file) {
|
||||
mySource = file;
|
||||
}
|
||||
|
||||
//<editor-fold desc="Implementation">
|
||||
private final File mySource;
|
||||
private Enumeration<? extends ZipEntry> myEntries;
|
||||
private ZipEntry myEntry;
|
||||
|
||||
@Override
|
||||
protected ZipInputStream openStream() throws IOException {
|
||||
return new ZipInputStream(new FileInputStream(mySource));
|
||||
protected ZipFile openStream() throws IOException {
|
||||
return new ZipFile(mySource);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Entry nextEntry(ZipInputStream zip) throws IOException {
|
||||
ZipEntry zipEntry = zip.getNextEntry();
|
||||
return zipEntry == null ? null : new Entry(zipEntry.getName(), zipEntry.isDirectory());
|
||||
protected Entry nextEntry(ZipFile zip) {
|
||||
if (myEntries == null) myEntries = zip.entries();
|
||||
myEntry = myEntries.hasMoreElements() ? myEntries.nextElement() : null;
|
||||
return myEntry == null ? null : new Entry(myEntry.getName(), myEntry.isDirectory());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InputStream openEntryStream(ZipInputStream stream, Entry entry) {
|
||||
return stream;
|
||||
protected InputStream openEntryStream(ZipFile zip, Entry entry) throws IOException {
|
||||
return zip.getInputStream(myEntry);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeEntryStream(InputStream stream) { }
|
||||
|
||||
@Override
|
||||
protected void closeStream(ZipInputStream stream) throws IOException {
|
||||
protected void closeEntryStream(InputStream stream) throws IOException {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeStream(ZipFile zip) throws IOException {
|
||||
zip.close();
|
||||
}
|
||||
//</editor-fold>
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipException
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
class DecompressorTest {
|
||||
@@ -62,6 +63,14 @@ class DecompressorTest {
|
||||
assertThat(File(dir, "root.txt")).exists()
|
||||
}
|
||||
|
||||
@Test(expected = ZipException::class)
|
||||
fun failsOnCorruptedZip() {
|
||||
val zip = tempDir.newFile("test.zip")
|
||||
zip.writeText("whatever")
|
||||
val dir = tempDir.newFolder("unpacked")
|
||||
Decompressor.Zip(zip).extract(dir)
|
||||
}
|
||||
|
||||
private fun writeEntry(zip: ZipOutputStream, name: String) {
|
||||
val entry = ZipEntry(name)
|
||||
entry.time = System.currentTimeMillis()
|
||||
|
||||
Reference in New Issue
Block a user