List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getExternalAttributes
public long getExternalAttributes()
From source file:at.spardat.xma.xdelta.JarDelta.java
/** * Entry to new name./*from w w w . j ava 2s .c o m*/ * * @param source the source * @param name the name * @return the zip archive entry * @throws ZipException the zip exception */ public static ZipArchiveEntry entryToNewName(ZipArchiveEntry source, String name) throws ZipException { if (source.getName().equals(name)) return new ZipArchiveEntry(source); ZipArchiveEntry ret = new ZipArchiveEntry(name); byte[] extra = source.getExtra(); if (extra != null) { ret.setExtraFields(ExtraFieldUtils.parse(extra, true, ExtraFieldUtils.UnparseableExtraField.READ)); } else { ret.setExtra(ExtraFieldUtils.mergeLocalFileDataData(source.getExtraFields(true))); } ret.setInternalAttributes(source.getInternalAttributes()); ret.setExternalAttributes(source.getExternalAttributes()); ret.setExtraFields(source.getExtraFields(true)); ret.setCrc(source.getCrc()); ret.setMethod(source.getMethod()); ret.setSize(source.getSize()); return ret; }
From source file:com.facebook.buck.util.ProjectFilesystemTest.java
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. File exe = tmp.newFile("test.exe"); exe.setExecutable(true);//from w w w . java 2s . com // Archive it into a zipfile using `ProjectFileSystem.createZip`. File zipFile = new File(tmp.getRoot().toPath().toString() + "/test.zip"); filesystem.createZip(ImmutableList.of(exe.toPath()), zipFile); // Now unpack the archive (using apache's common-compress, as it preserves // executable permissions) and verify that the archive entry has executable // permissions. try (ZipFile zip = new ZipFile(zipFile)) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); assertTrue(entries.hasMoreElements()); ZipArchiveEntry entry = entries.nextElement(); Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE)); assertFalse(entries.hasMoreElements()); } }
From source file:com.facebook.buck.io.ProjectFilesystemTest.java
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. Path exe = tmp.newFile("test.exe"); MoreFiles.makeExecutable(exe);//w ww .j a v a 2 s. c o m // Archive it into a zipfile using `ProjectFileSystem.createZip`. Path zipFile = tmp.getRoot().resolve("test.zip"); filesystem.createZip(ImmutableList.of(exe), zipFile); // Now unpack the archive (using apache's common-compress, as it preserves // executable permissions) and verify that the archive entry has executable // permissions. try (ZipFile zip = new ZipFile(zipFile.toFile())) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); assertTrue(entries.hasMoreElements()); ZipArchiveEntry entry = entries.nextElement(); Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE)); assertFalse(entries.hasMoreElements()); } }
From source file:com.facebook.buck.util.unarchive.UnzipTest.java
@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);/*from w w w.jav a 2s . c o m*/ 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")); }
From source file:at.spardat.xma.xdelta.JarPatcher.java
/** * Entry to new name.//from w w w .j a v a 2s . c o m * * @param source the source * @param name the name * @return the zip archive entry * @throws ZipException the zip exception */ private ZipArchiveEntry copyEntry(ZipArchiveEntry source) throws ZipException { ZipArchiveEntry ret = new ZipArchiveEntry(source.getName()); byte[] extra = source.getExtra(); if (extra != null) { ret.setExtraFields(ExtraFieldUtils.parse(extra, true, ExtraFieldUtils.UnparseableExtraField.READ)); } else { ret.setExtra(ExtraFieldUtils.mergeLocalFileDataData(source.getExtraFields(true))); } ret.setInternalAttributes(source.getInternalAttributes()); ret.setExternalAttributes(source.getExternalAttributes()); ret.setExtraFields(source.getExtraFields(true)); return ret; }
From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. Path exe = tmp.newFile("test.exe"); MostFiles.makeExecutable(exe);/*from ww w.ja v a 2s .c om*/ // Archive it into a zipfile using `Zip.create`. Path zipFile = tmp.getRoot().resolve("test.zip"); Zip.create(filesystem, ImmutableList.of(exe), zipFile); // Now unpack the archive (using apache's common-compress, as it preserves // executable permissions) and verify that the archive entry has executable // permissions. try (ZipFile zip = new ZipFile(zipFile.toFile())) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); assertTrue(entries.hasMoreElements()); ZipArchiveEntry entry = entries.nextElement(); Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE)); assertFalse(entries.hasMoreElements()); } }
From source file:com.facebook.buck.util.unarchive.Unzip.java
private void writeZipContents(ZipFile zip, ZipArchiveEntry entry, ProjectFilesystem filesystem, Path target) throws IOException { // Write file try (InputStream is = zip.getInputStream(entry)) { if (entry.isUnixSymlink()) { filesystem.createSymLink(target, filesystem.getPath(new String(ByteStreams.toByteArray(is), Charsets.UTF_8)), /* force */ true); } else {/*from www . j a v a 2 s .com*/ try (OutputStream out = filesystem.newFileOutputStream(target)) { ByteStreams.copy(is, out); } } } Path filePath = filesystem.resolve(target); File file = filePath.toFile(); // restore mtime for the file file.setLastModified(entry.getTime()); // TODO(simons): Implement what the comment below says we should do. // // Sets the file permissions of the output file given the information in {@code entry}'s // extra data field. According to the docs at // http://www.opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld there // are two extensions that might support file permissions: Acorn and ASi UNIX. We shall // assume that inputs are not from an Acorn SparkFS. The relevant section from the docs: // // <pre> // The following is the layout of the ASi extra block for Unix. The // local-header and central-header versions are identical. // (Last Revision 19960916) // // Value Size Description // ----- ---- ----------- // (Unix3) 0x756e Short tag for this extra block type ("nu") // TSize Short total data size for this block // CRC Long CRC-32 of the remaining data // Mode Short file permissions // SizDev Long symlink'd size OR major/minor dev num // UID Short user ID // GID Short group ID // (var.) variable symbolic link filename // // Mode is the standard Unix st_mode field from struct stat, containing // user/group/other permissions, setuid/setgid and symlink info, etc. // </pre> // // From the stat man page, we see that the following mask values are defined for the file // permissions component of the st_mode field: // // <pre> // S_ISUID 0004000 set-user-ID bit // S_ISGID 0002000 set-group-ID bit (see below) // S_ISVTX 0001000 sticky bit (see below) // // S_IRWXU 00700 mask for file owner permissions // // S_IRUSR 00400 owner has read permission // S_IWUSR 00200 owner has write permission // S_IXUSR 00100 owner has execute permission // // S_IRWXG 00070 mask for group permissions // S_IRGRP 00040 group has read permission // S_IWGRP 00020 group has write permission // S_IXGRP 00010 group has execute permission // // S_IRWXO 00007 mask for permissions for others // (not in group) // S_IROTH 00004 others have read permission // S_IWOTH 00002 others have write permission // S_IXOTH 00001 others have execute permission // </pre> // // For the sake of our own sanity, we're going to assume that no-one is using symlinks, // but we'll check and throw if they are. // // Before we do anything, we should check the header ID. Pfft! // // Having jumped through all these hoops, it turns out that InfoZIP's "unzip" store the // values in the external file attributes of a zip entry (found in the zip's central // directory) assuming that the OS creating the zip was one of an enormous list that // includes UNIX but not Windows, it first searches for the extra fields, and if not found // falls through to a code path that supports MS-DOS and which stores the UNIX file // attributes in the upper 16 bits of the external attributes field. // // We'll support neither approach fully, but we encode whether this file was executable // via storing 0100 in the fields that are typically used by zip implementations to store // POSIX permissions. If we find it was executable, use the platform independent java // interface to make this unpacked file executable. Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); if (permissions.contains(PosixFilePermission.OWNER_EXECUTE) && file.getCanonicalFile().exists()) { MostFiles.makeExecutable(filePath); } }
From source file:com.facebook.buck.zip.Unzip.java
/** * Unzips a file to a destination and returns the paths of the written files. *///from ww w . j a va2s .c o m public static ImmutableList<Path> extractZipFile(Path zipFile, ProjectFilesystem filesystem, Path relativePath, ExistingFileMode existingFileMode) throws IOException { // if requested, clean before extracting if (existingFileMode == ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES) { try (ZipFile zip = new ZipFile(zipFile.toFile())) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); filesystem.deleteRecursivelyIfExists(relativePath.resolve(entry.getName())); } } } ImmutableList.Builder<Path> filesWritten = ImmutableList.builder(); try (ZipFile zip = new ZipFile(zipFile.toFile())) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String fileName = entry.getName(); Path target = relativePath.resolve(fileName); // TODO(bolinfest): Keep track of which directories have already been written to avoid // making unnecessary Files.createDirectories() calls. In practice, a single zip file will // have many entries in the same directory. if (entry.isDirectory()) { // Create the directory and all its parent directories filesystem.mkdirs(target); } else { // Create parent folder filesystem.createParentDirs(target); filesWritten.add(target); // Write file try (InputStream is = zip.getInputStream(entry)) { if (entry.isUnixSymlink()) { filesystem.createSymLink(target, filesystem.getPath(new String(ByteStreams.toByteArray(is), Charsets.UTF_8)), /* force */ true); } else { try (OutputStream out = filesystem.newFileOutputStream(target)) { ByteStreams.copy(is, out); } } } // restore mtime for the file filesystem.resolve(target).toFile().setLastModified(entry.getTime()); // TODO(shs96c): Implement what the comment below says we should do. // // Sets the file permissions of the output file given the information in {@code entry}'s // extra data field. According to the docs at // http://www.opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld there // are two extensions that might support file permissions: Acorn and ASi UNIX. We shall // assume that inputs are not from an Acorn SparkFS. The relevant section from the docs: // // <pre> // The following is the layout of the ASi extra block for Unix. The // local-header and central-header versions are identical. // (Last Revision 19960916) // // Value Size Description // ----- ---- ----------- // (Unix3) 0x756e Short tag for this extra block type ("nu") // TSize Short total data size for this block // CRC Long CRC-32 of the remaining data // Mode Short file permissions // SizDev Long symlink'd size OR major/minor dev num // UID Short user ID // GID Short group ID // (var.) variable symbolic link filename // // Mode is the standard Unix st_mode field from struct stat, containing // user/group/other permissions, setuid/setgid and symlink info, etc. // </pre> // // From the stat man page, we see that the following mask values are defined for the file // permissions component of the st_mode field: // // <pre> // S_ISUID 0004000 set-user-ID bit // S_ISGID 0002000 set-group-ID bit (see below) // S_ISVTX 0001000 sticky bit (see below) // // S_IRWXU 00700 mask for file owner permissions // // S_IRUSR 00400 owner has read permission // S_IWUSR 00200 owner has write permission // S_IXUSR 00100 owner has execute permission // // S_IRWXG 00070 mask for group permissions // S_IRGRP 00040 group has read permission // S_IWGRP 00020 group has write permission // S_IXGRP 00010 group has execute permission // // S_IRWXO 00007 mask for permissions for others // (not in group) // S_IROTH 00004 others have read permission // S_IWOTH 00002 others have write permission // S_IXOTH 00001 others have execute permission // </pre> // // For the sake of our own sanity, we're going to assume that no-one is using symlinks, // but we'll check and throw if they are. // // Before we do anything, we should check the header ID. Pfft! // // Having jumped through all these hoops, it turns out that InfoZIP's "unzip" store the // values in the external file attributes of a zip entry (found in the zip's central // directory) assuming that the OS creating the zip was one of an enormous list that // includes UNIX but not Windows, it first searches for the extra fields, and if not found // falls through to a code path that supports MS-DOS and which stores the UNIX file // attributes in the upper 16 bits of the external attributes field. // // We'll support neither approach fully, but we encode whether this file was executable // via storing 0100 in the fields that are typically used by zip implementations to store // POSIX permissions. If we find it was executable, use the platform independent java // interface to make this unpacked file executable. Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); if (permissions.contains(PosixFilePermission.OWNER_EXECUTE)) { MoreFiles.makeExecutable(filesystem.resolve(target)); } } } } return filesWritten.build(); }