use of java.util.zip.ZipEntry in project j2objc by google.
the class AbstractZipFileTest method writeEntries.
/**
* Compresses the given number of files, each of the given size, into a .zip archive.
*/
protected void writeEntries(ZipOutputStream out, int entryCount, long entrySize, boolean setEntrySize) throws IOException {
byte[] writeBuffer = new byte[8192];
Random random = new Random();
try {
for (int entry = 0; entry < entryCount; ++entry) {
ZipEntry ze = new ZipEntry(Integer.toHexString(entry));
if (setEntrySize) {
ze.setSize(entrySize);
}
out.putNextEntry(ze);
for (long i = 0; i < entrySize; i += writeBuffer.length) {
random.nextBytes(writeBuffer);
int byteCount = (int) Math.min(writeBuffer.length, entrySize - i);
out.write(writeBuffer, 0, byteCount);
}
out.closeEntry();
}
} finally {
out.close();
}
}
use of java.util.zip.ZipEntry in project j2objc by google.
the class AbstractZipFileTest method testSTORED.
public void testSTORED() throws IOException {
ZipOutputStream out = createZipOutputStream(createTemporaryZipFile());
CRC32 crc = new CRC32();
// Missing CRC, size, and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing CRC and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing CRC and size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setSize(0);
ze.setCompressedSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing size and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Missing size is copied from compressed size.
{
ZipEntry ze = new ZipEntry("okay1");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
assertEquals(-1, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
ze.setCompressedSize(0);
assertEquals(-1, ze.getSize());
assertEquals(0, ze.getCompressedSize());
out.putNextEntry(ze);
assertEquals(0, ze.getSize());
assertEquals(0, ze.getCompressedSize());
}
// Missing compressed size is copied from size.
{
ZipEntry ze = new ZipEntry("okay2");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
assertEquals(-1, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
ze.setSize(0);
assertEquals(0, ze.getSize());
assertEquals(-1, ze.getCompressedSize());
out.putNextEntry(ze);
assertEquals(0, ze.getSize());
assertEquals(0, ze.getCompressedSize());
}
// Mismatched size and compressed size => failure.
try {
ZipEntry ze = new ZipEntry("a");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
ze.setCompressedSize(1);
ze.setSize(0);
out.putNextEntry(ze);
fail();
} catch (ZipException expected) {
}
// Everything present => success.
ZipEntry ze = new ZipEntry("okay");
ze.setMethod(ZipEntry.STORED);
ze.setCrc(crc.getValue());
ze.setSize(0);
ze.setCompressedSize(0);
out.putNextEntry(ze);
out.close();
}
use of java.util.zip.ZipEntry in project j2objc by google.
the class AbstractZipFileTest method test_getComment_unset.
public void test_getComment_unset() throws Exception {
File file = createTemporaryZipFile();
ZipOutputStream out = createZipOutputStream(file);
ZipEntry ze = new ZipEntry("test entry");
ze.setComment("per-entry comment");
out.putNextEntry(ze);
out.close();
ZipFile zipFile = new ZipFile(file);
assertEquals(null, zipFile.getComment());
}
use of java.util.zip.ZipEntry in project j2objc by google.
the class AbstractZipFileTest method testZipFileErrorReadingData.
public void testZipFileErrorReadingData() throws IOException {
File resources = Support_Resources.createTempFolder();
File tempZipFile = Support_Resources.copyFile(resources, "java/util/zip", "ZipFileBreak.zip");
String tempZipFilePath = tempZipFile.getAbsolutePath();
try (ZipFile zipFile = new ZipFile(tempZipFilePath)) {
ZipEntry entry = zipFile.getEntry("subdir/file.txt");
assertNotNull(entry);
byte[] content = toByteArray(zipFile.getInputStream(entry));
assertNotNull(content);
entry = zipFile.getEntry("subdir/file.pb");
assertNotNull(entry);
content = toByteArray(zipFile.getInputStream(entry));
assertNotNull(content);
}
}
use of java.util.zip.ZipEntry in project j2objc by google.
the class OldAndroidZipStreamTest method createCompressedZip.
private static void createCompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
ZipOutputStream out = new ZipOutputStream(bytesOut);
try {
int i;
for (i = 0; i < 3; i++) {
byte[] input = makeSampleFile(i);
ZipEntry newEntry = new ZipEntry("file-" + i);
if (i != 1)
newEntry.setComment("this is file " + i);
out.putNextEntry(newEntry);
out.write(input, 0, input.length);
out.closeEntry();
}
out.setComment("This is a lovely compressed archive!");
} finally {
out.close();
}
}
Aggregations