List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getUnixMode
public int getUnixMode()
From source file:com.android.tradefed.util.ZipUtil2.java
/** * A util method to apply unix mode from {@link ZipArchiveEntry} to the created local file * system entry if necessary//from w w w .ja v a 2s .c o m * @param entry the entry inside zipfile (potentially contains mode info) * @param localFile the extracted local file entry * @throws IOException */ private static void applyUnixModeIfNecessary(ZipArchiveEntry entry, File localFile) throws IOException { if (entry.getPlatform() == ZipArchiveEntry.PLATFORM_UNIX) { Files.setPosixFilePermissions(localFile.toPath(), FileUtil.unixModeToPosix(entry.getUnixMode())); } else { CLog.i("Entry does not contain Unix mode info: %s", entry.getName()); } }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
/** * Extracts a {@code *.zip} archive into the target folder. * @param monitor the progress monitor/*ww w. j a v a 2s.c om*/ * @param archiveFile the archive file * @param targetDirectory the target folder * @throws IOException if failed to extract the archive */ public static void extractZip(IProgressMonitor monitor, File archiveFile, File targetDirectory) throws IOException { SubMonitor sub = SubMonitor.convert(monitor, Messages.IoUtils_monitorExtractZip, 10); try { ZipFile zip = new ZipFile(archiveFile); try { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); if (entry.isDirectory()) { createDirectory(targetDirectory, entry); } else { InputStream input = zip.getInputStream(entry); try { File file = createFile(targetDirectory, entry, input); setFileMode(file, entry.getUnixMode()); } finally { input.close(); } sub.worked(1); sub.setWorkRemaining(10); } } } finally { zip.close(); } } finally { if (monitor != null) { monitor.done(); } } }
From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java
private void copySymlink(Path file, String filenameInZip) throws IOException { logger.finer("Adding symlink: " + file + " with filename: " + filenameInZip); Path symlinkTarget = Files.readSymbolicLink(file); // Unfortunately, there is no API method to create a symlink in a ZIP file, // however, a symlink entry can easily be created by hand. // The requirements for a symlink entry are: // - the unix mode must have the LINK_FLAG set // - the content must contain the target of the symlink as UTF8 string ZipArchiveEntry entry = new ZipArchiveEntry(filenameInZip); entry.setUnixMode(entry.getUnixMode() | UnixStat.LINK_FLAG); zipStream.putArchiveEntry(entry);//from w w w.ja va 2 s. com zipStream.write(symlinkTarget.toString().getBytes(StandardCharsets.UTF_8)); zipStream.closeArchiveEntry(); }
From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java
private void copyDirectory(String filenameInZip) throws IOException { logger.finer("Adding directory: " + filenameInZip); // entries ending in / indicate a directory ZipArchiveEntry entry = new ZipArchiveEntry(filenameInZip + "/"); // in addition, set the unix directory flag entry.setUnixMode(entry.getUnixMode() | UnixStat.DIR_FLAG); zipStream.putArchiveEntry(entry);// w ww. j a v a 2 s .com zipStream.closeArchiveEntry(); }
From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java
/** * Unzip a zip file, notifying the given monitor along the way. *///from w w w . j a v a 2 s .c o m public static void unzip(File zipFile, File destination, String taskName, IProgressMonitor monitor) throws IOException { ZipFile zip = new ZipFile(zipFile); //TODO (pquitslund): add real progress units if (monitor != null) { monitor.beginTask(taskName, 1); } Enumeration<ZipArchiveEntry> e = zip.getEntries(); while (e.hasMoreElements()) { ZipArchiveEntry entry = e.nextElement(); File file = new File(destination, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { InputStream is = zip.getInputStream(entry); File parent = file.getParentFile(); if (parent != null && parent.exists() == false) { parent.mkdirs(); } FileOutputStream os = new FileOutputStream(file); try { IOUtils.copy(is, os); } finally { os.close(); is.close(); } file.setLastModified(entry.getTime()); int mode = entry.getUnixMode(); if ((mode & EXEC_MASK) != 0) { file.setExecutable(true); } } } //TODO (pquitslund): fix progress units if (monitor != null) { monitor.worked(1); monitor.done(); } }
From source file:com.facebook.buck.zip.ZipStepTest.java
@Test public void willRecurseIntoSubdirectories() throws IOException { Path parent = tmp.newFolder("zipstep"); Path out = parent.resolve("output.zip"); Path toZip = tmp.newFolder("zipdir"); Files.createFile(toZip.resolve("file1.txt")); Files.createDirectories(toZip.resolve("child")); Files.createFile(toZip.resolve("child/file2.txt")); ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.DEFAULT_COMPRESSION_LEVEL, Paths.get("zipdir")); assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode()); // Make sure we have the right attributes. try (ZipFile zip = new ZipFile(out.toFile())) { ZipArchiveEntry entry = zip.getEntry("child/"); assertNotEquals(entry.getUnixMode() & MoreFiles.S_IFDIR, 0); }//from www. jav a 2 s. co m try (Zip zip = new Zip(out, false)) { assertEquals(ImmutableSet.of("file1.txt", "child/file2.txt"), zip.getFileNames()); } }
From source file:io.webfolder.cdp.ChromiumDownloader.java
private static void unpack(File archive, File destionation) throws IOException { try (ZipFile zip = new ZipFile(archive)) { Map<File, String> symLinks = new LinkedHashMap<>(); Enumeration<ZipArchiveEntry> iterator = zip.getEntries(); // Top directory name we are going to ignore String parentDirectory = iterator.nextElement().getName(); // Iterate files & folders while (iterator.hasMoreElements()) { ZipArchiveEntry entry = iterator.nextElement(); String name = entry.getName().substring(parentDirectory.length()); File outputFile = new File(destionation, name); if (name.startsWith("interactive_ui_tests")) { continue; }/* www . ja v a 2 s. co m*/ if (entry.isUnixSymlink()) { symLinks.put(outputFile, zip.getUnixSymlink(entry)); } else if (!entry.isDirectory()) { if (!outputFile.getParentFile().isDirectory()) { outputFile.getParentFile().mkdirs(); } try (FileOutputStream outStream = new FileOutputStream(outputFile)) { IOUtils.copy(zip.getInputStream(entry), outStream); } } // Set permission if (!entry.isUnixSymlink() && outputFile.exists()) try { Files.setPosixFilePermissions(outputFile.toPath(), modeToPosixPermissions(entry.getUnixMode())); } catch (Exception e) { // ignore } } for (Map.Entry<File, String> entry : symLinks.entrySet()) { try { Path source = Paths.get(entry.getKey().getAbsolutePath()); Path target = source.getParent().resolve(entry.getValue()); if (!source.toFile().exists()) Files.createSymbolicLink(source, target); } catch (Exception e) { // ignore } } } }
From source file:net.orpiske.ssps.common.archive.zip.ZipArchive.java
/** * Decompress a file/*w w w . ja v a2s .c om*/ * @param source the source file to be uncompressed * @param destination the destination directory * @return the number of bytes read * @throws IOException for lower level I/O errors */ public long unpack(File source, File destination) throws IOException { ZipFile zipFile = null; long ret = 0; try { zipFile = new ZipFile(source); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File outFile = new File(destination.getPath() + File.separator + entry.getName()); CompressedArchiveUtils.prepareDestination(outFile); if (!entry.isDirectory()) { ret += extractFile(zipFile, entry, outFile); } int mode = entry.getUnixMode(); PermissionsUtils.setPermissions(mode, outFile); } } finally { if (zipFile != null) { zipFile.close(); } } return ret; }
From source file:com.android.repository.util.InstallerUtilTest.java
public void testUnzip() throws Exception { if (new MockFileOp().isWindows()) { // can't run on windows. return;//from w w w.j a v a 2 s. c o m } // zip needs a real file, so no MockFileOp for us. FileOp fop = FileOpUtils.create(); Path root = Files.createTempDirectory("InstallerUtilTest"); Path outRoot = Files.createTempDirectory("InstallerUtilTest"); try { Path file1 = root.resolve("foo"); Files.write(file1, "content".getBytes()); Path dir1 = root.resolve("bar"); Files.createDirectories(dir1); Path file2 = dir1.resolve("baz"); Files.write(file2, "content2".getBytes()); Files.createSymbolicLink(root.resolve("link1"), dir1); Files.createSymbolicLink(root.resolve("link2"), file2); Path outZip = outRoot.resolve("out.zip"); try (ZipArchiveOutputStream out = new ZipArchiveOutputStream(outZip.toFile()); Stream<Path> listing = Files.walk(root)) { listing.forEach(path -> { try { ZipArchiveEntry archiveEntry = (ZipArchiveEntry) out.createArchiveEntry(path.toFile(), root.relativize(path).toString()); out.putArchiveEntry(archiveEntry); if (Files.isSymbolicLink(path)) { archiveEntry.setUnixMode(UnixStat.LINK_FLAG | archiveEntry.getUnixMode()); out.write(path.getParent().relativize(Files.readSymbolicLink(path)).toString() .getBytes()); } else if (!Files.isDirectory(path)) { out.write(Files.readAllBytes(path)); } out.closeArchiveEntry(); } catch (Exception e) { fail(); } }); } Path unzipped = outRoot.resolve("unzipped"); Files.createDirectories(unzipped); InstallerUtil.unzip(outZip.toFile(), unzipped.toFile(), fop, 1, new FakeProgressIndicator()); assertEquals("content", new String(Files.readAllBytes(unzipped.resolve("foo")))); Path resultDir = unzipped.resolve("bar"); Path resultFile2 = resultDir.resolve("baz"); assertEquals("content2", new String(Files.readAllBytes(resultFile2))); Path resultLink = unzipped.resolve("link1"); assertTrue(Files.isDirectory(resultLink)); assertTrue(Files.isSymbolicLink(resultLink)); assertTrue(Files.isSameFile(resultLink, resultDir)); Path resultLink2 = unzipped.resolve("link2"); assertEquals("content2", new String(Files.readAllBytes(resultLink2))); assertTrue(Files.isSymbolicLink(resultLink2)); assertTrue(Files.isSameFile(resultLink2, resultFile2)); } finally { fop.deleteFileOrFolder(root.toFile()); fop.deleteFileOrFolder(outRoot.toFile()); } }
From source file:com.android.sdklib.internal.repository.packages.Package.java
/** * Hook called right after a file has been unzipped (during an install). * <p/>/*from w w w .j av a 2s . co m*/ * The base class implementation makes sure to properly adjust set executable * permission on Linux and MacOS system if the zip entry was marked as +x. * * @param archive The archive that is being installed. * @param monitor The {@link ITaskMonitor} to display errors. * @param fileOp The {@link IFileOp} used by the archive installer. * @param unzippedFile The file that has just been unzipped in the install temp directory. * @param zipEntry The {@link ZipArchiveEntry} that has just been unzipped. */ public void postUnzipFileHook(Archive archive, ITaskMonitor monitor, IFileOp fileOp, File unzippedFile, ZipArchiveEntry zipEntry) { // if needed set the permissions. if (sUsingUnixPerm && fileOp.isFile(unzippedFile)) { // get the mode and test if it contains the executable bit int mode = zipEntry.getUnixMode(); if ((mode & 0111) != 0) { try { fileOp.setExecutablePermission(unzippedFile); } catch (IOException ignore) { } } } }