Search in sources :

Example 56 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testZip64.

@Test
public void testZip64() throws IOException {
    // Generated with: 'echo "foo" > entry; zip -fz -q out.zip entry'
    byte[] data = new byte[] { 0x50, 0x4b, 0x03, 0x04, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, (byte) 0x86, (byte) 0xa6, 0x46, (byte) 0xa8, 0x65, 0x32, 0x7e, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x05, 0x00, 0x30, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x55, 0x54, 0x09, 0x00, 0x03, (byte) 0xb2, 0x7e, 0x4a, 0x55, (byte) 0xb2, 0x7e, 0x4a, 0x55, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x46, 0x3a, 0x04, 0x00, 0x04, (byte) 0x88, 0x13, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, (byte) 0x86, (byte) 0xa6, 0x46, (byte) 0xa8, 0x65, 0x32, 0x7e, 0x04, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x05, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, (byte) 0xa0, (byte) 0x81, 0x00, 0x00, 0x00, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x55, 0x54, 0x05, 0x00, 0x03, (byte) 0xb2, 0x7e, 0x4a, 0x55, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x46, 0x3a, 0x04, 0x00, 0x04, (byte) 0x88, 0x13, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x06, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, (byte) 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x57, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00 };
    try (FileOutputStream out = new FileOutputStream(test)) {
        out.write(data);
    }
    String foo = "foo\n";
    byte[] expectedFooData = foo.getBytes(UTF_8);
    ExtraDataList extras = new ExtraDataList();
    extras.add(new ExtraData((short) 0x0001, ZipUtil.longToLittleEndian(expectedFooData.length)));
    byte[] extra = extras.getBytes();
    CRC32 crc = new CRC32();
    crc.reset();
    crc.update(expectedFooData);
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        ZipFileEntry fooEntry = reader.getEntry("entry");
        InputStream fooIn = reader.getInputStream(fooEntry);
        byte[] fooData = new byte[expectedFooData.length];
        fooIn.read(fooData);
        assertThat(fooData).isEqualTo(expectedFooData);
        assertThat(fooEntry.getName()).isEqualTo("entry");
        assertThat(fooEntry.getComment()).isEqualTo("");
        assertThat(fooEntry.getMethod()).isEqualTo(Compression.STORED);
        assertThat(fooEntry.getVersionNeeded()).isEqualTo(Feature.ZIP64_SIZE.getMinVersion());
        assertThat(fooEntry.getSize()).isEqualTo(expectedFooData.length);
        assertThat(fooEntry.getCompressedSize()).isEqualTo(expectedFooData.length);
        assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
        assertThat(fooEntry.getExtra().get((short) 0x0001).getBytes()).isEqualTo(extra);
    }
}
Also used : CRC32(java.util.zip.CRC32) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test)

Example 57 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testRawFileData.

@Test
public void testRawFileData() throws IOException {
    CRC32 crc = new CRC32();
    Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
        ZipEntry foo = new ZipEntry("foo");
        foo.setComment("foo comment.");
        foo.setMethod(ZipEntry.DEFLATED);
        zout.putNextEntry(foo);
        zout.write("foo".getBytes(UTF_8));
        zout.closeEntry();
        ZipEntry bar = new ZipEntry("bar");
        bar.setComment("bar comment.");
        bar.setMethod(ZipEntry.STORED);
        bar.setSize("bar".length());
        bar.setCompressedSize("bar".length());
        crc.reset();
        crc.update("bar".getBytes(UTF_8));
        bar.setCrc(crc.getValue());
        zout.putNextEntry(bar);
        zout.write("bar".getBytes(UTF_8));
        zout.closeEntry();
    }
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        ZipFileEntry fooEntry = reader.getEntry("foo");
        InputStream fooIn = reader.getRawInputStream(fooEntry);
        byte[] fooData = new byte[10];
        fooIn.read(fooData);
        byte[] expectedFooData = new byte[10];
        deflator.reset();
        deflator.setInput("foo".getBytes(UTF_8));
        deflator.finish();
        deflator.deflate(expectedFooData);
        assertThat(fooData).isEqualTo(expectedFooData);
        ZipFileEntry barEntry = reader.getEntry("bar");
        InputStream barIn = reader.getRawInputStream(barEntry);
        byte[] barData = new byte[3];
        barIn.read(barData);
        byte[] expectedBarData = "bar".getBytes(UTF_8);
        assertThat(barData).isEqualTo(expectedBarData);
        assertThat(barIn.read()).isEqualTo(-1);
        assertThat(barIn.read(barData)).isEqualTo(-1);
        assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
        thrown.expect(IOException.class);
        thrown.expectMessage("Reset is not supported on this type of stream.");
        barIn.reset();
    }
}
Also used : Deflater(java.util.zip.Deflater) CRC32(java.util.zip.CRC32) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 58 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testMalformed_ShorterThanSignature.

@Test
public void testMalformed_ShorterThanSignature() throws IOException {
    try (FileOutputStream out = new FileOutputStream(test)) {
        out.write(new byte[] { 1, 2, 3 });
    }
    thrown.expect(ZipException.class);
    thrown.expectMessage("is malformed. It does not contain an end of central directory record.");
    new ZipReader(test, UTF_8).close();
}
Also used : FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test)

Example 59 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testFileData.

@Test
public void testFileData() throws IOException {
    CRC32 crc = new CRC32();
    try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
        ZipEntry foo = new ZipEntry("foo");
        foo.setComment("foo comment.");
        foo.setMethod(ZipEntry.DEFLATED);
        zout.putNextEntry(foo);
        zout.write("foo".getBytes(UTF_8));
        zout.closeEntry();
        ZipEntry bar = new ZipEntry("bar");
        bar.setComment("bar comment.");
        bar.setMethod(ZipEntry.STORED);
        bar.setSize("bar".length());
        bar.setCompressedSize("bar".length());
        crc.reset();
        crc.update("bar".getBytes(UTF_8));
        bar.setCrc(crc.getValue());
        zout.putNextEntry(bar);
        zout.write("bar".getBytes(UTF_8));
        zout.closeEntry();
    }
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        ZipFileEntry fooEntry = reader.getEntry("foo");
        InputStream fooIn = reader.getInputStream(fooEntry);
        byte[] fooData = new byte[3];
        fooIn.read(fooData);
        byte[] expectedFooData = "foo".getBytes(UTF_8);
        assertThat(fooData).isEqualTo(expectedFooData);
        assertThat(fooIn.read()).isEqualTo(-1);
        assertThat(fooIn.read(fooData)).isEqualTo(-1);
        assertThat(fooIn.read(fooData, 0, 3)).isEqualTo(-1);
        ZipFileEntry barEntry = reader.getEntry("bar");
        InputStream barIn = reader.getInputStream(barEntry);
        byte[] barData = new byte[3];
        barIn.read(barData);
        byte[] expectedBarData = "bar".getBytes(UTF_8);
        assertThat(barData).isEqualTo(expectedBarData);
        assertThat(barIn.read()).isEqualTo(-1);
        assertThat(barIn.read(barData)).isEqualTo(-1);
        assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
        thrown.expect(IOException.class);
        thrown.expectMessage("Reset is not supported on this type of stream.");
        barIn.reset();
    }
}
Also used : CRC32(java.util.zip.CRC32) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 60 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testMalformed_Empty.

@Test
public void testMalformed_Empty() throws IOException {
    try (FileOutputStream out = new FileOutputStream(test)) {
    }
    thrown.expect(ZipException.class);
    thrown.expectMessage("is malformed. It does not contain an end of central directory record.");
    new ZipReader(test, UTF_8).close();
}
Also used : FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test)

Aggregations

FileOutputStream (java.io.FileOutputStream)6586 File (java.io.File)3769 IOException (java.io.IOException)2864 FileInputStream (java.io.FileInputStream)1189 OutputStream (java.io.OutputStream)1071 BufferedOutputStream (java.io.BufferedOutputStream)907 InputStream (java.io.InputStream)864 FileNotFoundException (java.io.FileNotFoundException)682 OutputStreamWriter (java.io.OutputStreamWriter)644 Test (org.junit.Test)528 BufferedWriter (java.io.BufferedWriter)313 PrintWriter (java.io.PrintWriter)311 ZipEntry (java.util.zip.ZipEntry)311 BufferedInputStream (java.io.BufferedInputStream)287 DataOutputStream (java.io.DataOutputStream)275 ZipOutputStream (java.util.zip.ZipOutputStream)273 ByteArrayOutputStream (java.io.ByteArrayOutputStream)267 ArrayList (java.util.ArrayList)247 Writer (java.io.Writer)215 URL (java.net.URL)206