Java Code Examples for org.apache.commons.compress.archivers.zip.ZipArchiveEntry#setMethod()

The following examples show how to use org.apache.commons.compress.archivers.zip.ZipArchiveEntry#setMethod() . 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: ScatterZipOutputStream.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
void writeTo(ZipArchiveOutputStream target) throws IOException {
    entryStore.closeForWriting();
    backingStore.closeForWriting();
    String line;

    try (InputStream stream = backingStore.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(entryStore.getInputStream(), StandardCharsets.UTF_8))) {
        while ((line = reader.readLine()) != null) {
            String[] values = line.split(",");
            if (values.length != 5)
                throw new IOException("Failed to read temporary zip entry.");

            ZipArchiveEntry entry = new ZipArchiveEntry(values[0]);
            entry.setMethod(Integer.parseInt(values[1]));
            entry.setCrc(Long.parseLong(values[2]));
            entry.setCompressedSize(Long.parseLong(values[3]));
            entry.setSize(Long.parseLong(values[4]));

            try (BoundedInputStream rawStream = new BoundedInputStream(stream, entry.getCompressedSize())) {
                target.addRawArchiveEntry(entry, rawStream);
            }
        }
    } catch (NumberFormatException e) {
        throw new IOException("Failed to read temporary zip entry.", e);
    }
}
 
Example 2
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractZipFilePreservesExecutePermissionsAndModificationTime()
    throws InterruptedException, IOException {

  // getFakeTime returs time with some non-zero millis. By doing division and multiplication by
  // 1000 we get rid of that.
  long time = ZipConstants.getFakeTime() / 1000 * 1000;

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("test.exe");
    entry.setUnixMode(
        (int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
    entry.setSize(DUMMY_FILE_CONTENTS.length);
    entry.setMethod(ZipEntry.STORED);
    entry.setTime(time);
    zip.putArchiveEntry(entry);
    zip.write(DUMMY_FILE_CONTENTS);
    zip.closeArchiveEntry();
  }

  // Now run `Unzip.extractZipFile` on our test zip and verify that the file is executable.
  Path extractFolder = tmpFolder.newFolder();
  ImmutableList<Path> result =
      ArchiveFormat.ZIP
          .getUnarchiver()
          .extractArchive(
              new DefaultProjectFilesystemFactory(),
              zipFile.toAbsolutePath(),
              extractFolder.toAbsolutePath(),
              ExistingFileMode.OVERWRITE);
  Path exe = extractFolder.toAbsolutePath().resolve("test.exe");
  assertTrue(Files.exists(exe));
  assertThat(Files.getLastModifiedTime(exe).toMillis(), Matchers.equalTo(time));
  assertTrue(Files.isExecutable(exe));
  assertEquals(ImmutableList.of(extractFolder.resolve("test.exe")), result);
}
 
Example 3
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractSymlink() throws InterruptedException, IOException {
  assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("link.txt");
    entry.setUnixMode((int) MostFiles.S_IFLNK);
    String target = "target.txt";
    entry.setSize(target.getBytes(Charsets.UTF_8).length);
    entry.setMethod(ZipEntry.STORED);
    zip.putArchiveEntry(entry);
    zip.write(target.getBytes(Charsets.UTF_8));
    zip.closeArchiveEntry();
  }

  Path extractFolder = tmpFolder.newFolder();

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          new DefaultProjectFilesystemFactory(),
          zipFile.toAbsolutePath(),
          extractFolder.toAbsolutePath(),
          ExistingFileMode.OVERWRITE);
  Path link = extractFolder.toAbsolutePath().resolve("link.txt");
  assertTrue(Files.isSymbolicLink(link));
  assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
 
Example 4
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractBrokenSymlinkWithOwnerExecutePermissions()
    throws InterruptedException, IOException {
  assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("link.txt");
    entry.setUnixMode((int) MostFiles.S_IFLNK);
    String target = "target.txt";
    entry.setSize(target.getBytes(Charsets.UTF_8).length);
    entry.setMethod(ZipEntry.STORED);

    // Mark the file as being executable.
    Set<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_EXECUTE);

    long externalAttributes =
        entry.getExternalAttributes() + (MorePosixFilePermissions.toMode(filePermissions) << 16);
    entry.setExternalAttributes(externalAttributes);

    zip.putArchiveEntry(entry);
    zip.write(target.getBytes(Charsets.UTF_8));
    zip.closeArchiveEntry();
  }

  Path extractFolder = tmpFolder.newFolder();

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          new DefaultProjectFilesystemFactory(),
          zipFile.toAbsolutePath(),
          extractFolder.toAbsolutePath(),
          ExistingFileMode.OVERWRITE);
  Path link = extractFolder.toAbsolutePath().resolve("link.txt");
  assertTrue(Files.isSymbolicLink(link));
  assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
 
Example 5
Source File: UnzipTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testStripsPrefixAndIgnoresSiblings() throws IOException {
  byte[] bazDotSh = "echo \"baz.sh\"\n".getBytes(Charsets.UTF_8);
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    zip.putArchiveEntry(new ZipArchiveEntry("foo"));
    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("foo/bar/baz.txt"));
    zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
    zip.closeArchiveEntry();

    ZipArchiveEntry exeEntry = new ZipArchiveEntry("foo/bar/baz.sh");
    exeEntry.setUnixMode(
        (int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
    exeEntry.setMethod(ZipEntry.STORED);
    exeEntry.setSize(bazDotSh.length);
    zip.putArchiveEntry(exeEntry);
    zip.write(bazDotSh);

    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("sibling"));
    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("sibling/some/dir/and/file.txt"));
    zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
    zip.closeArchiveEntry();
  }

  Path extractFolder = Paths.get("output_dir", "nested");

  ProjectFilesystem filesystem =
      TestProjectFilesystems.createProjectFilesystem(tmpFolder.getRoot());

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          zipFile,
          filesystem,
          extractFolder,
          Optional.of(Paths.get("foo")),
          ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);

  assertFalse(filesystem.isDirectory(extractFolder.resolve("sibling")));
  assertFalse(filesystem.isDirectory(extractFolder.resolve("foo")));
  assertFalse(filesystem.isDirectory(extractFolder.resolve("some")));

  Path bazDotTxtPath = extractFolder.resolve("bar").resolve("baz.txt");
  Path bazDotShPath = extractFolder.resolve("bar").resolve("baz.sh");

  assertTrue(filesystem.isDirectory(extractFolder.resolve("bar")));
  assertTrue(filesystem.isFile(bazDotTxtPath));
  assertTrue(filesystem.isFile(bazDotShPath));
  assertTrue(filesystem.isExecutable(bazDotShPath));
  assertEquals(new String(bazDotSh), filesystem.readFileIfItExists(bazDotShPath).get());
  assertEquals(
      new String(DUMMY_FILE_CONTENTS), filesystem.readFileIfItExists(bazDotTxtPath).get());
}