Skip to content

Instantly share code, notes, and snippets.

@martiner
Last active August 29, 2015 13:56
Show Gist options
  • Save martiner/9004316 to your computer and use it in GitHub Desktop.
Save martiner/9004316 to your computer and use it in GitHub Desktop.
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.Assert.assertFalse;
public class ZipTest {
public static final String ZIP_FILE = "file.zip";
public static final String FILE = "file.txt";
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
private File root;
private File dir;
private File file;
@Before
public void setUp() throws Exception {
root = tmp.getRoot();
dir = tmp.newFolder();
file = createZipFile(dir);
}
@Test
public void shouldFailIfRootCreatedInRootDir() throws Exception {
try {
YourZipLibrary.extractZip(file, dir); // todo
} catch (Exception ignored) { }
assertFalse(new File(root, FILE).exists());
}
public File createZipFile(File dir) throws IOException {
final File file = new File(dir, ZIP_FILE);
try (final ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file))) {
output.putNextEntry(new ZipEntry("../" + FILE));
final byte[] bytes = "Hello world".getBytes();
output.write(bytes, 0, bytes.length);
return file;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment