List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry setUnixMode
public void setUnixMode(int mode)
From source file:com.sic.bb.jenkins.plugins.sicci_for_xcode.util.ExtendedFile.java
private static void zipDirectory(File file, String path, ZipArchiveOutputStream zipStream) throws IOException, InterruptedException { if (path == null) path = new String(); else if (!path.isEmpty()) path += File.separatorChar; ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, path + file.getName()); zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode()); /* TODO: archiving symlinks doesn't work atm zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode()); // w w w .java2s . co m if(Util.isSymlink(file)) { zipEntry = new ZipArchiveEntry(path + file.getName()); zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode()); AsiExtraField field = new AsiExtraField(); field.setLinkedFile(path + file.getName()); zipEntry.addExtraField(field); zipStream.putArchiveEntry(zipEntry); zipStream.closeArchiveEntry(); return; } */ zipStream.putArchiveEntry(zipEntry); if (!file.isDirectory()) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); Util.copyStream(fileInputStream, zipStream); } finally { IOUtils.closeQuietly(fileInputStream); zipStream.closeArchiveEntry(); } } else { zipStream.closeArchiveEntry(); String[] entries = file.list(); if (entries != null) for (String entry : entries) zipDirectory(new File(file, entry), path + file.getName(), zipStream); } }
From source file:com.ibm.streamsx.topology.internal.context.remote.ZippedToolkitRemoteContext.java
private static void addAllToZippedArchive(Map<Path, String> starts, Path zipFilePath) throws IOException { try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(zipFilePath.toFile())) { for (Path start : starts.keySet()) { final String rootEntryName = starts.get(start); Files.walkFileTree(start, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { // Skip pyc files. if (file.getFileName().toString().endsWith(".pyc")) 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; }//from ww w . j av a2 s. co m // 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; } public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { final String dirName = dir.getFileName().toString(); // Don't include pyc files or .toolkit if (dirName.equals("__pycache__")) return FileVisitResult.SKIP_SUBTREE; ZipArchiveEntry dirEntry = new ZipArchiveEntry(dir.toFile(), rootEntryName + "/" + start.relativize(dir).toString().replace(File.separatorChar, '/') + "/"); zos.putArchiveEntry(dirEntry); zos.closeArchiveEntry(); return FileVisitResult.CONTINUE; } }); } } }
From source file:com.excelsiorjet.maven.plugin.JetMojo.java
private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out) throws IOException { File[] files = new File(sourceDir).listFiles(); assert files != null; for (File file : files) { if (file.isDirectory()) { compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out); } else {/*from w w w . j av a 2s .co m*/ ZipArchiveEntry entry = new ZipArchiveEntry(file.getAbsolutePath().substring(rootDir.length() + 1)); if (Utils.isUnix() && file.canExecute()) { entry.setUnixMode(0100777); } out.putArchiveEntry(entry); InputStream in = new BufferedInputStream( new FileInputStream(sourceDir + File.separator + file.getName())); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.closeArchiveEntry(); } } }
From source file:com.gitblit.utils.CompressionUtils.java
/** * Zips the contents of the tree at the (optionally) specified revision and * the (optionally) specified basepath to the supplied outputstream. * //from ww w. j av a 2s .c o m * @param repository * @param basePath * if unspecified, entire repository is assumed. * @param objectId * if unspecified, HEAD is assumed. * @param os * @return true if repository was successfully zipped to supplied output * stream */ public static boolean zip(Repository repository, String basePath, String objectId, OutputStream os) { RevCommit commit = JGitUtils.getCommit(repository, objectId); if (commit == null) { return false; } boolean success = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { tw.reset(); tw.addTree(commit.getTree()); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setComment("Generated by Gitblit"); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); ObjectReader reader = tw.getObjectReader(); long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString()); entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB)); entry.setComment(commit.getName()); entry.setUnixMode(mode.getBits()); entry.setTime(modified); zos.putArchiveEntry(entry); ObjectLoader ldr = repository.open(id); ldr.copyTo(zos); zos.closeArchiveEntry(); } zos.finish(); success = true; } catch (IOException e) { error(e, repository, "{0} failed to zip files from commit {1}", commit.getName()); } finally { tw.release(); rw.dispose(); } return success; }
From source file:com.excelsiorjet.api.util.Utils.java
private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out) throws IOException { File[] files = new File(sourceDir).listFiles(); assert files != null; for (File file : files) { if (file.isDirectory()) { compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out); } else {//from ww w. j a va 2s. c om ZipArchiveEntry entry = new ZipArchiveEntry(file.getAbsolutePath().substring(rootDir.length() + 1)); if (Host.isUnix()) { if (file.canExecute()) { //set -rwxr-xr-x entry.setUnixMode(0100755); } else { //set -rw-r--r-- entry.setUnixMode(0100644); } } out.putArchiveEntry(entry); try (InputStream in = new BufferedInputStream( new FileInputStream(sourceDir + File.separator + file.getName()))) { copy(in, out); } out.closeArchiveEntry(); } } }
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 . j av a 2 s . co m zipStream.write(symlinkTarget.toString().getBytes(StandardCharsets.UTF_8)); zipStream.closeArchiveEntry(); }
From source file:hudson.util.io.ZipArchiver.java
@Override public void visitSymlink(final File f, final String target, final String relativePath) throws IOException { int mode = IOUtils.lmode(f); ZipArchiveEntry zae = new ZipArchiveEntry(relativePath); if (mode != -1) { zae.setUnixMode(mode); }/*from w w w .j a v a 2 s . com*/ zae.setTime(f.lastModified()); zip.putArchiveEntry(zae); zip.write(target.getBytes(StandardCharsets.UTF_8), 0, target.length()); zip.closeArchiveEntry(); entriesWritten++; }
From source file:hudson.util.io.ZipArchiver.java
public void visit(final File f, final String relativePath) throws IOException { int mode = IOUtils.mode(f); // ZipArchiveEntry already covers all the specialities we used to handle here: // - Converts backslashes to slashes // - Handles trailing slash of directories // - Sets entry's time from file // - Sets bitmask from setUnixMode() argument. ZipArchiveEntry zae = new ZipArchiveEntry(f, relativePath); if (mode != -1) { zae.setUnixMode(mode); }//from w w w. j a v a2 s . com zip.putArchiveEntry(zae); if (!zae.isDirectory()) { FileInputStream in = new FileInputStream(f); try { int len; while ((len = in.read(buf)) >= 0) { zip.write(buf, 0, len); } } finally { in.close(); } } zip.closeArchiveEntry(); entriesWritten++; }
From source file:com.facebook.buck.zip.UnzipTest.java
@Test public void testExtractSymlink() throws 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) MoreFiles.S_IFLNK); String target = "target.txt"; entry.setSize(target.getBytes(Charsets.UTF_8).length); entry.setMethod(ZipEntry.STORED); zip.putArchiveEntry(entry);/* w ww . j a v a 2 s. c o m*/ zip.write(target.getBytes(Charsets.UTF_8)); zip.closeArchiveEntry(); } // Now run `Unzip.extractZipFile` on our test zip and verify that the file is executable. Path extractFolder = tmpFolder.newFolder(); Unzip.extractZipFile(zipFile.toAbsolutePath(), extractFolder.toAbsolutePath(), Unzip.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:com.facebook.buck.zip.UnzipTest.java
@Test public void testExtractZipFilePreservesExecutePermissionsAndModificationTime() throws IOException { // getFakeTime returs time with some non-zero millis. By doing division and multiplication by // 1000 we get rid of that. final 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);// w ww . java2 s. c o m 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 = Unzip.extractZipFile(zipFile.toAbsolutePath(), extractFolder.toAbsolutePath(), Unzip.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); }