Skip to content

Instantly share code, notes, and snippets.

@andreybpanfilov
Last active July 26, 2017 04:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andreybpanfilov/f38796d7f31cac62d86334035542a9e3 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipFileTest {
public static void main(String[] args) throws Exception {
new ZipFileTest().crashReadWrite();
new ZipFileTest().crashReadTouch();
}
public ZipFileTest() throws Exception {
}
public void crashReadTouch() throws InterruptedException, IOException {
File file = new File(System.getProperty("java.io.tmpdir")
+ "/zipfile.zip ");
new ZipFileWriter(file).run();
for (int i = 0; i <= 200; i++) {
ZipFile zip = new ZipFile(System.getProperty("java.io.tmpdir")
+ "/zipfile.zip ");
new Thread(new ZipFileReader(zip)).start();
Thread.sleep(10);
file.setLastModified(System.currentTimeMillis());
}
}
public void crashReadWrite() throws InterruptedException, IOException {
File file = new File(System.getProperty("java.io.tmpdir")
+ "/zipfile.zip ");
new ZipFileWriter(file).run();
for (int i = 0; i <= 10; i++) {
ZipFile zip = new ZipFile(System.getProperty("java.io.tmpdir")
+ "/zipfile.zip ");
new Thread(new ZipFileReader(zip)).start();
file = new File(System.getProperty("java.io.tmpdir")
+ "/zipfile.zip ");
new Thread(new ZipFileWriter(file)).start();
}
}
public class ZipFileReader implements Runnable {
private ZipFile zipFile;
public ZipFileReader(ZipFile aZip) {
zipFile = aZip;
}
@Override
public void run() {
Enumeration zipEntries = zipFile.entries();
for (int i = 1000; i >= 0; i--) {
// getEntry will crash vm if zip file was modified
zipFile.getEntry("test" + i);
}
while (zipEntries.hasMoreElements()) {
// getNextEntry will crash vm if zip file was modified
ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
zipEntry.getCrc();
}
}
}
public class ZipFileWriter implements Runnable {
private File file;
public ZipFileWriter(File aFile) {
file = aFile;
}
@Override
public void run() {
int i = 1;
ZipOutputStream zOut;
File tmp;
try {
tmp = File.createTempFile(ZipFileTest.class.getSimpleName(),
null, file.getParentFile());
zOut = new ZipOutputStream(new FileOutputStream(tmp));
} catch (IOException e1) {
e1.printStackTrace();
return;
}
try {
for (int j = 0; j <= 1000; j++) {
ZipEntry e = new ZipEntry("test" + j);
zOut.putNextEntry(e);
String content = "test" + i++;
for (i = 0; i <= 1000; i++) {
zOut.write(content.getBytes());
}
zOut.closeEntry();
}
} catch (IOException e) {
System.err.println("Caught Exception: " + e);
e.printStackTrace();
} finally {
try {
zOut.close();
if(!tmp.renameTo(file)) {
System.err.println("Can't rename");
}
} catch (IOException e) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment