Java Code Examples for java.util.zip.ZipEntry#setExtra()

The following examples show how to use java.util.zip.ZipEntry#setExtra() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public void addMimeTypeFile(String statedPath, String actualPath) throws IOException  {
  //  byte data[] = new byte[BUFFER];
    CRC32 crc = new CRC32();
    
  //  FileInputStream fi = new FileInputStream(actualPath);
  //  BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
    out.setLevel(0);
    ZipEntry entry = new ZipEntry(statedPath);
    entry.setExtra(null);
    names.add(statedPath);
    String contents = "application/epub+zip";
    crc.update(contents.getBytes());
    entry.setCompressedSize(contents.length());
    entry.setSize(contents.length());
    entry.setCrc(crc.getValue());
    entry.setMethod(ZipEntry.STORED);
    out.putNextEntry(entry);
 //   int count;
//    while ((count = origin.read(data, 0, BUFFER)) != -1) {
//      out.write(data, 0, count);
//    }
  //  origin.close();
    out.write(contents.getBytes(),0,contents.length());
    out.setLevel(Deflater.BEST_COMPRESSION);
  }
 
Example 2
Source File: ArchiveBleach.java    From DocBleach with MIT License 6 votes vote down vote up
private ZipEntry cloneEntry(ZipEntry entry) {
  ZipEntry newEntry = new ZipEntry(entry.getName());

  newEntry.setTime(entry.getTime());
  if (entry.getCreationTime() != null) {
    newEntry.setCreationTime(entry.getCreationTime());
  }
  if (entry.getLastModifiedTime() != null) {
    newEntry.setLastModifiedTime(entry.getLastModifiedTime());
  }
  if (entry.getLastAccessTime() != null) {
    newEntry.setLastAccessTime(entry.getLastAccessTime());
  }
  newEntry.setComment(entry.getComment());
  newEntry.setExtra(entry.getExtra());

  return newEntry;
}
 
Example 3
Source File: ZipEntryTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.ZipEntry#clone()
 */
public void test_clone() {
    // Test for method java.util.zip.ZipEntry.clone()
    Object obj = zentry.clone();
    assertEquals("toString()", zentry.toString(), obj.toString());
    assertEquals("hashCode()", zentry.hashCode(), obj.hashCode());

    // One constructor
    ZipEntry zeInput = new ZipEntry("InputZIP");
    byte[] extraB = { 'a', 'b', 'd', 'e' };
    zeInput.setExtra(extraB);
    assertEquals(extraB, zeInput.getExtra());
    assertEquals(extraB[3], zeInput.getExtra()[3]);
    assertEquals(extraB.length, zeInput.getExtra().length);

    // test Clone()
    ZipEntry zeOutput = (ZipEntry) zeInput.clone();
    assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]);
    assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length);
    assertEquals(extraB[3], zeOutput.getExtra()[3]);
    assertEquals(extraB.length, zeOutput.getExtra().length);
}
 
Example 4
Source File: ZipEntryTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testMaxLengthExtra() throws Exception {
  byte[] maxLengthExtra = new byte[65535];

  File f = createTemporaryZipFile();
  ZipOutputStream out = createZipOutputStream(f);
  ZipEntry ze = new ZipEntry("x");
  ze.setSize(0);
  ze.setTime(ENTRY_TIME);
  ze.setExtra(maxLengthExtra);
  out.putNextEntry(ze);
  out.closeEntry();
  out.close();

  // Read it back, and check that we see the entry.
  ZipFile zipFile = new ZipFile(f);
  assertEquals(maxLengthExtra.length, zipFile.getEntry("x").getExtra().length);
  zipFile.close();
}
 
Example 5
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test public void testZipEntryInvalidTime() throws IOException {
  long date = 312796800000L; // 11/30/1979 00:00:00, which is also 0 in DOS format
  byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    foo.setTime(date);
    foo.setExtra(extra);
    zout.putNextEntry(foo);
    zout.write("foo".getBytes(UTF_8));
    zout.closeEntry();
  }

  try (ZipReader reader = new ZipReader(test, UTF_8)) {
    ZipFileEntry fooEntry = reader.getEntry("foo");
    assertThat(fooEntry.getTime()).isEqualTo(ZipUtil.DOS_EPOCH);
  }
}
 
Example 6
Source File: ZipUtils.java    From Stark with Apache License 2.0 5 votes vote down vote up
public static void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze)
        throws IOException {
    ZipEntry ze2 = new ZipEntry(ze.getName());
    ze2.setMethod(ze.getMethod());
    ze2.setTime(ze.getTime());
    ze2.setComment(ze.getComment());
    ze2.setExtra(ze.getExtra());
    if (ze.getMethod() == ZipEntry.STORED) {
        ze2.setSize(ze.getSize());
        ze2.setCrc(ze.getCrc());
    }
    os.putNextEntry(ze2);
    writeBytes(zf, ze, os);
}
 
Example 7
Source File: BaseTest.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
/**
 * 构建 zip 文件
 */
public static void generateZip() throws IOException {
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(getTempDemoZip()));
    CRC32 crc = new CRC32();

    /* name end with '/' indicates a directory */
    jos.putNextEntry(new ZipEntry("META-INF/"));

    jos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
    jos.write(generateManifest());

    jos.putNextEntry(new ZipEntry("lib/"));

    ZipEntry jarEntry = new ZipEntry("lib/junit-4.12.jar");
    byte[] jarContent = fetchResource("junit-4.12.jar");
    crc.update(jarContent);

    jarEntry.setMethod(ZipEntry.STORED);
    jarEntry.setSize(jarContent.length);
    jarEntry.setCrc(crc.getValue());

    jos.putNextEntry(jarEntry);
    jos.write(jarContent);

    ZipEntry entryForTest = new ZipEntry(TEST_ENTRY);
    entryForTest.setComment(TEST_ENTRY_COMMENT);
    entryForTest.setExtra(TEST_ENTRY_EXTRA.getBytes());
    jos.putNextEntry(entryForTest);

    jos.closeEntry();
    jos.close();
}
 
Example 8
Source File: JarSigner.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze)
        throws IOException {
    ZipEntry ze2 = new ZipEntry(ze.getName());
    ze2.setMethod(ze.getMethod());
    ze2.setTime(ze.getTime());
    ze2.setComment(ze.getComment());
    ze2.setExtra(ze.getExtra());
    if (ze.getMethod() == ZipEntry.STORED) {
        ze2.setSize(ze.getSize());
        ze2.setCrc(ze.getCrc());
    }
    os.putNextEntry(ze2);
    writeBytes(zf, ze, os);
}
 
Example 9
Source File: RejarClassesForAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ZipEntry newZipEntry(ZipEntry ze) {
    ZipEntry ze2 = new ZipEntry(ze.getName());
    ze2.setComment(ze.getComment());
    ze2.setTime(ze.getTime());
    ze2.setExtra(ze.getExtra());
    return ze2;
}
 
Example 10
Source File: ZipEntryTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testTooLongExtra() throws Exception {
  byte[] tooLongExtra = new byte[65536];
  ZipEntry ze = new ZipEntry("x");
  try {
    ze.setExtra(tooLongExtra);
    fail();
  } catch (IllegalArgumentException expected) {
  }
}
 
Example 11
Source File: Instrumenter.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * This method instruments each class file in a given jar and write the
 * instrumented one to the destination jar file
 * </p>
 * 
 * @param zis
 *            Stream for input jar
 * @param zos
 *            Stream for output jar
 
 *             If any error occurred
 */
public void instrumentJar(ZipInputStream zis, ZipOutputStream zos)
		throws IOException {
	ZipEntry entry;
	byte[] outputBytes = null;

	// execute for each entry in the jar
	while ((entry = zis.getNextEntry()) != null) {
		outputBytes = InstrumentUtil.getEntryBytesFromZip(zis);

		
		// instrument if and only if it is a class file
		if (entry.getName().endsWith(
				InstrumentConstants.CLASS_FILE_EXTENSION)) {				
			currentlyInstrumentingClass=entry.getName().replace('/', '.');
			currentlyInstrumentingClass=currentlyInstrumentingClass.substring(0,currentlyInstrumentingClass.indexOf(".class"));				

			outputBytes = instrumentEntry(outputBytes);
						}

		// create a new entry and write the bytes obtained above
		ZipEntry outputEntry = new ZipEntry(entry.getName());
		outputEntry.setComment(entry.getComment());
		outputEntry.setExtra(entry.getExtra());
		outputEntry.setTime(entry.getTime());
		zos.putNextEntry(outputEntry);
		zos.write(outputBytes);
		zos.closeEntry();
		zis.closeEntry();
	}

	// adding other files if required
	addAdditionalFiles(zos);
}
 
Example 12
Source File: ZipEntryReader.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
static ZipEntry readEntry(ByteBuffer in) throws IOException {

        int sig = in.getInt();
        if (sig != CENSIG) {
             throw new ZipException("Central Directory Entry not found");
        }

        in.position(8);
        int gpbf = in.getShort() & 0xffff;

        if ((gpbf & GPBF_UNSUPPORTED_MASK) != 0) {
            throw new ZipException("Invalid General Purpose Bit Flag: " + gpbf);
        }

        int compressionMethod = in.getShort() & 0xffff;
        int time = in.getShort() & 0xffff;
        int modDate = in.getShort() & 0xffff;

        // These are 32-bit values in the file, but 64-bit fields in this object.
        long crc = ((long) in.getInt()) & 0xffffffffL;
        long compressedSize = ((long) in.getInt()) & 0xffffffffL;
        long size = ((long) in.getInt()) & 0xffffffffL;

        int nameLength = in.getShort() & 0xffff;
        int extraLength = in.getShort() & 0xffff;
        int commentByteCount = in.getShort() & 0xffff;

        // This is a 32-bit value in the file, but a 64-bit field in this object.
        in.position(42);
        long localHeaderRelOffset = ((long) in.getInt()) & 0xffffffffL;

        byte[] nameBytes = new byte[nameLength];
        in.get(nameBytes, 0, nameBytes.length);
        String name = new String(nameBytes, 0, nameBytes.length, UTF_8);

        ZipEntry entry = new ZipEntry(name);
        entry.setMethod(compressionMethod);
        entry.setTime(getTime(time, modDate));

        entry.setCrc(crc);
        entry.setCompressedSize(compressedSize);
        entry.setSize(size);

        // The RI has always assumed UTF-8. (If GPBF_UTF8_FLAG isn't set, the encoding is
        // actually IBM-437.)
        if (commentByteCount > 0) {
            byte[] commentBytes = new byte[commentByteCount];
            in.get(commentBytes, 0, commentByteCount);
            entry.setComment(new String(commentBytes, 0, commentBytes.length, UTF_8));
        }

        if (extraLength > 0) {
            byte[] extra = new byte[extraLength];
            in.get(extra, 0, extraLength);
            entry.setExtra(extra);
        }

        return entry;

    }
 
Example 13
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test public void testZipEntryFields() throws IOException {
  CRC32 crc = new CRC32();
  Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  long date = 791784306000L; // 2/3/1995 04:05:06
  byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
  byte[] tmp = new byte[128];
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {

    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    foo.setTime(date);
    foo.setExtra(extra);
    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");
    assertThat(fooEntry.getName()).isEqualTo("foo");
    assertThat(fooEntry.getComment()).isEqualTo("foo comment.");
    assertThat(fooEntry.getMethod()).isEqualTo(Compression.DEFLATED);
    assertThat(fooEntry.getVersion()).isEqualTo(Compression.DEFLATED.getMinVersion());
    assertThat(fooEntry.getTime()).isEqualTo(date);
    assertThat(fooEntry.getSize()).isEqualTo("foo".length());
    deflater.reset();
    deflater.setInput("foo".getBytes(UTF_8));
    deflater.finish();
    assertThat(fooEntry.getCompressedSize()).isEqualTo(deflater.deflate(tmp));
    crc.reset();
    crc.update("foo".getBytes(UTF_8));
    assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
    assertThat(fooEntry.getExtra().getBytes()).isEqualTo(extra);

    ZipFileEntry barEntry = reader.getEntry("bar");
    assertThat(barEntry.getName()).isEqualTo("bar");
    assertThat(barEntry.getComment()).isEqualTo("bar comment.");
    assertThat(barEntry.getMethod()).isEqualTo(Compression.STORED);
    assertThat(barEntry.getVersion()).isEqualTo(Compression.STORED.getMinVersion());
    assertDateAboutNow(new Date(barEntry.getTime()));
    assertThat(barEntry.getSize()).isEqualTo("bar".length());
    assertThat(barEntry.getCompressedSize()).isEqualTo("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    assertThat(barEntry.getCrc()).isEqualTo(crc.getValue());
    assertThat(barEntry.getExtra().getBytes()).isEqualTo(new byte[] {});
  }
}