List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry setMethod
public void setMethod(int method)
From source file:at.spardat.xma.xdelta.JarDelta.java
/** * Entry to new name.//from w w w. ja va 2 s. 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.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);/* www.j a v a2 s . co 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);//from w w w. java 2 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); }
From source file:com.facebook.buck.util.unarchive.UnzipTest.java
@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);//from w w w. j a v a 2 s .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:com.facebook.buck.util.unarchive.UnzipTest.java
@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();//from w w w. j a v a 2s . c o m 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()); }
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. j av a2s. 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:com.facebook.buck.util.unarchive.UnzipTest.java
@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);//from w w w . jav a 2s. com 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); }
From source file:ee.sk.digidoc.SignedDoc.java
/** * Writes the SignedDoc to an output file * and automatically calculates DataFile sizes * and digests//from w w w. j ava 2s. co m * @param outputFile output file name * @param fTempSdoc temporrary file, copy of original for copying items * @throws DigiDocException for all errors */ public void writeToStream(OutputStream os/*, File fTempSdoc*/) throws DigiDocException { DigiDocException ex1 = validateFormatAndVersion(); if (ex1 != null) throw ex1; try { DigiDocXmlGenFactory genFac = new DigiDocXmlGenFactory(this); if (m_format.equals(SignedDoc.FORMAT_BDOC)) { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding("UTF-8"); if (m_logger.isDebugEnabled()) m_logger.debug("OS: " + ((os != null) ? "OK" : "NULL")); // write mimetype if (m_logger.isDebugEnabled()) m_logger.debug("Writing: " + MIMET_FILE_NAME); ZipArchiveEntry ze = new ZipArchiveEntry(MIMET_FILE_NAME); if (m_comment == null) m_comment = DigiDocGenFactory.getUserInfo(m_format, m_version); ze.setComment(m_comment); ze.setMethod(ZipArchiveEntry.STORED); java.util.zip.CRC32 crc = new java.util.zip.CRC32(); if (m_version.equals(BDOC_VERSION_1_0)) { ze.setSize(SignedDoc.MIMET_FILE_CONTENT_10.getBytes().length); crc.update(SignedDoc.MIMET_FILE_CONTENT_10.getBytes()); } if (m_version.equals(BDOC_VERSION_1_1)) { ze.setSize(SignedDoc.MIMET_FILE_CONTENT_11.getBytes().length); crc.update(SignedDoc.MIMET_FILE_CONTENT_11.getBytes()); } if (m_version.equals(BDOC_VERSION_2_1)) { ze.setSize(SignedDoc.MIMET_FILE_CONTENT_20.getBytes().length); crc.update(SignedDoc.MIMET_FILE_CONTENT_20.getBytes()); } ze.setCrc(crc.getValue()); zos.putArchiveEntry(ze); if (m_version.equals(BDOC_VERSION_1_0)) { zos.write(SignedDoc.MIMET_FILE_CONTENT_10.getBytes()); } if (m_version.equals(BDOC_VERSION_1_1)) { zos.write(SignedDoc.MIMET_FILE_CONTENT_11.getBytes()); } if (m_version.equals(BDOC_VERSION_2_1)) { zos.write(SignedDoc.MIMET_FILE_CONTENT_20.getBytes()); } zos.closeArchiveEntry(); // write manifest.xml if (m_logger.isDebugEnabled()) m_logger.debug("Writing: " + MANIF_FILE_NAME); ze = new ZipArchiveEntry(MANIF_DIR_META_INF); ze = new ZipArchiveEntry(MANIF_FILE_NAME); ze.setComment(DigiDocGenFactory.getUserInfo(m_format, m_version)); zos.putArchiveEntry(ze); //if(m_logger.isDebugEnabled()) // m_logger.debug("Writing manif:\n" + m_manifest.toString()); zos.write(m_manifest.toXML()); zos.closeArchiveEntry(); // write data files for (int i = 0; i < countDataFiles(); i++) { DataFile df = getDataFile(i); if (m_logger.isDebugEnabled()) m_logger.debug("Writing DF: " + df.getFileName() + " content: " + df.getContentType() + " df-cache: " + ((df.getDfCacheFile() != null) ? df.getDfCacheFile().getAbsolutePath() : "NONE")); InputStream is = null; if (df.hasAccessToDataFile()) is = df.getBodyAsStream(); else is = findDataFileAsStream(df.getFileName()); if (is != null) { File dfFile = new File(df.getFileName()); String fileName = dfFile.getName(); ze = new ZipArchiveEntry(fileName); if (df.getComment() == null) df.setComment(DigiDocGenFactory.getUserInfo(m_format, m_version)); ze.setComment(df.getComment()); ze.setSize(dfFile.length()); ze.setTime( (df.getLastModDt() != null) ? df.getLastModDt().getTime() : dfFile.lastModified()); zos.putArchiveEntry(ze); byte[] data = new byte[2048]; int nRead = 0, nTotal = 0; crc = new java.util.zip.CRC32(); while ((nRead = is.read(data)) > 0) { zos.write(data, 0, nRead); nTotal += nRead; crc.update(data, 0, nRead); } ze.setSize(nTotal); ze.setCrc(crc.getValue()); zos.closeArchiveEntry(); is.close(); } } for (int i = 0; i < countSignatures(); i++) { Signature sig = getSignature(i); String sFileName = sig.getPath(); if (sFileName == null) { if (m_version.equals(BDOC_VERSION_2_1)) sFileName = SIG_FILE_NAME_20 + (i + 1) + ".xml"; else sFileName = SIG_FILE_NAME + (i + 1) + ".xml"; } if (!sFileName.startsWith("META-INF")) sFileName = "META-INF/" + sFileName; if (m_logger.isDebugEnabled()) m_logger.debug("Writing SIG: " + sFileName + " orig: " + ((sig.getOrigContent() != null) ? "OK" : "NULL")); ze = new ZipArchiveEntry(sFileName); if (sig.getComment() == null) sig.setComment(DigiDocGenFactory.getUserInfo(m_format, m_version)); ze.setComment(sig.getComment()); String sSig = null; if (sig.getOrigContent() != null) sSig = new String(sig.getOrigContent(), "UTF-8"); else sSig = sig.toString(); if (sSig != null && !sSig.startsWith("<?xml")) sSig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + sSig; byte[] sdata = sSig.getBytes("UTF-8"); if (m_logger.isDebugEnabled()) m_logger.debug("Writing SIG: " + sFileName + " xml:\n---\n " + ((sSig != null) ? sSig : "NULL") + "\n---\n "); ze.setSize(sdata.length); crc = new java.util.zip.CRC32(); crc.update(sdata); ze.setCrc(crc.getValue()); zos.putArchiveEntry(ze); zos.write(sdata); zos.closeArchiveEntry(); } zos.close(); } else if (m_format.equals(SignedDoc.FORMAT_DIGIDOC_XML)) { // ddoc format os.write(xmlHeader().getBytes()); for (int i = 0; i < countDataFiles(); i++) { DataFile df = getDataFile(i); df.writeToFile(os); os.write("\n".getBytes()); } for (int i = 0; i < countSignatures(); i++) { Signature sig = getSignature(i); if (sig.getOrigContent() != null) os.write(sig.getOrigContent()); else os.write(genFac.signatureToXML(sig)); os.write("\n".getBytes()); } os.write(xmlTrailer().getBytes()); } } catch (DigiDocException ex) { throw ex; // allready handled } catch (Exception ex) { DigiDocException.handleException(ex, DigiDocException.ERR_WRITE_FILE); } }
From source file:org.apache.ant.compress.taskdefs.Zip.java
public Zip() { setFactory(new ZipStreamFactory() { public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException { ZipArchiveOutputStream o = (ZipArchiveOutputStream) super.getArchiveStream(stream, encoding); configure(o);/*ww w .j ava2s . co m*/ return o; } public ArchiveOutputStream getArchiveOutputStream(File f, String encoding) throws IOException { ZipArchiveOutputStream o = (ZipArchiveOutputStream) super.getArchiveOutputStream(f, encoding); configure(o); return o; } }); setEntryBuilder(new ArchiveBase.EntryBuilder() { public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) { boolean isDir = r.getResource().isDirectory(); ZipArchiveEntry ent = new ZipArchiveEntry(r.getName()); ent.setTime(round(r.getResource().getLastModified(), 2000)); ent.setSize(isDir ? 0 : r.getResource().getSize()); if (!isDir && r.getCollectionFlags().hasModeBeenSet()) { ent.setUnixMode(r.getCollectionFlags().getMode()); } else if (isDir && r.getCollectionFlags().hasDirModeBeenSet()) { ent.setUnixMode(r.getCollectionFlags().getDirMode()); } else if (r.getResourceFlags().hasModeBeenSet()) { ent.setUnixMode(r.getResourceFlags().getMode()); } else { ent.setUnixMode(isDir ? ArchiveFileSet.DEFAULT_DIR_MODE : ArchiveFileSet.DEFAULT_FILE_MODE); } if (r.getResourceFlags().getZipExtraFields() != null) { ent.setExtraFields(r.getResourceFlags().getZipExtraFields()); } if (keepCompression && r.getResourceFlags().hasCompressionMethod()) { ent.setMethod(r.getResourceFlags().getCompressionMethod()); } return ent; } }); setFileSetBuilder(new ArchiveBase.FileSetBuilder() { public ArchiveFileSet buildFileSet(Resource dest) { ArchiveFileSet afs = new ZipFileSet(); afs.setSrcResource(dest); return afs; } }); }
From source file:org.apache.openejb.maven.plugin.customizer.monkey.jar.JarPatcher.java
private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix) throws IOException { final String path = f.getPath().replace(prefix, "").replace(File.separator, "/"); final ZipArchiveEntry zip = new ZipArchiveEntry(f, path); zip.setMethod(method); final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip); jar.putArchiveEntry(archiveEntry);//from w w w.ja va 2 s .com if (f.isDirectory()) { jar.closeArchiveEntry(); final File[] files = f.listFiles(); if (files != null) { for (final File child : files) { jar(method, jar, child.getCanonicalFile(), prefix); } } } else { final InputStream is = new FileInputStream(f); IOUtils.copy(is, jar); is.close(); jar.closeArchiveEntry(); } }