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

The following examples show how to use org.apache.commons.compress.archivers.zip.ZipArchiveEntry#setUnixMode() . 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: GeneratorService.java    From vertx-starter with Apache License 2.0 6 votes vote down vote up
private void addFile(Path rootPath, Path filePath, ArchiveOutputStream stream) throws IOException {
  String relativePath = rootPath.relativize(filePath).toString();
  if (relativePath.length() == 0) return;
  String entryName = jarFileWorkAround(leadingDot(relativePath));
  ArchiveEntry entry = stream.createArchiveEntry(filePath.toFile(), entryName);
  if (EXECUTABLES.contains(entryName)) {
    if (entry instanceof ZipArchiveEntry) {
      ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) entry;
      zipArchiveEntry.setUnixMode(0744);
    } else if (entry instanceof TarArchiveEntry) {
      TarArchiveEntry tarArchiveEntry = (TarArchiveEntry) entry;
      tarArchiveEntry.setMode(0100744);
    }
  }
  stream.putArchiveEntry(entry);
  if (filePath.toFile().isFile()) {
    try (InputStream i = Files.newInputStream(filePath)) {
      IOUtils.copy(i, stream);
    }
  }
  stream.closeArchiveEntry();
}
 
Example 2
Source File: ZippedToolkitRemoteContext.java    From streamsx.topology with Apache License 2.0 6 votes vote down vote up
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    String fn = file.getFileName().toString();
    // Skip pyc and pyi files.
    if (fn.endsWith(".pyc") || fn.endsWith(".pyi"))
        return FileVisitResult.CONTINUE;
    
    String entryName = rootEntryName;
    String relativePath = start.relativize(file).toString();
    // If empty, file is the start file.
    if(!relativePath.isEmpty()){                          
        entryName = entryName + "/" + relativePath;
    }
    // Zip uses forward slashes
    entryName = entryName.replace(File.separatorChar, '/');
    
    ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), entryName);
    if (Files.isExecutable(file))
        entry.setUnixMode(0100770);
    else
        entry.setUnixMode(0100660);

    zos.putArchiveEntry(entry);
    Files.copy(file, zos);
    zos.closeArchiveEntry();
    return FileVisitResult.CONTINUE;
}
 
Example 3
Source File: Assembler.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the given file in the ZIP file. If the given file is a directory, then this method
 * recursively adds all files contained in this directory. This method is invoked for zipping
 * the "application/sis-console/src/main/artifact" directory and sub-directories before to zip.
 */
private void appendRecursively(final File file, String relativeFile, final ZipArchiveOutputStream out) throws IOException {
    if (file.isDirectory()) {
        relativeFile += '/';
    }
    final ZipArchiveEntry entry = new ZipArchiveEntry(file, relativeFile);
    if (file.canExecute()) {
        entry.setUnixMode(0744);
    }
    out.putArchiveEntry(entry);
    if (!entry.isDirectory()) {
        try (FileInputStream in = new FileInputStream(file)) {
            in.transferTo(out);
        }
    }
    out.closeArchiveEntry();
    if (entry.isDirectory()) {
        for (final String filename : file.list(this)) {
            appendRecursively(new File(file, filename), relativeFile.concat(filename), out);
        }
    }
}
 
Example 4
Source File: FunctionZipProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void addZipEntry(ZipArchiveOutputStream zip, Path path, String name, int mode) throws Exception {
    ZipArchiveEntry entry = (ZipArchiveEntry) zip.createArchiveEntry(path.toFile(), name);
    entry.setUnixMode(mode);
    zip.putArchiveEntry(entry);
    try (InputStream i = Files.newInputStream(path)) {
        IOUtils.copy(i, zip);
    }
    zip.closeArchiveEntry();
}
 
Example 5
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 6
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 7
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 8
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());
}