org.apache.commons.compress.archivers.zip.ZipFile Java Examples
The following examples show how to use
org.apache.commons.compress.archivers.zip.ZipFile.
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: DefaultProjectFilesystemTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. Path exe = tmp.newFile("test.exe"); MostFiles.makeExecutable(exe); // 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()); } }
Example #2
Source File: ZipRuleIntegrationTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void shouldUnpackContentsOfZipSources() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp(); Path zip = workspace.buildAndReturnOutput("//example:zipsources"); try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipArchiveEntry menu = zipFile.getEntry("menu.txt"); assertThat(menu, Matchers.notNullValue()); assertFalse(menu.isUnixSymlink()); assertFalse(menu.isDirectory()); ZipArchiveEntry cake = zipFile.getEntry("cake.txt"); assertThat(cake, Matchers.notNullValue()); assertFalse(cake.isUnixSymlink()); assertFalse(cake.isDirectory()); } }
Example #3
Source File: AbstractInitializrIntegrationTests.java From initializr with Apache License 2.0 | 6 votes |
private void unzip(Path archiveFile, Path project) throws IOException { try (ZipFile zip = new ZipFile(archiveFile.toFile())) { Enumeration<? extends ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); Path path = project.resolve(entry.getName()); if (entry.isDirectory()) { Files.createDirectories(path); } else { Files.createDirectories(path.getParent()); Files.write(path, StreamUtils.copyToByteArray(zip.getInputStream(entry))); } applyPermissions(path, getPosixFilePermissions(entry.getUnixMode())); } } }
Example #4
Source File: ExtensionUtils.java From ghidra with Apache License 2.0 | 6 votes |
/** * Given a zip file, returns the {@link Properties} defined in the embedded extension.properties file. * * @param file the extension archive file * @return the properties file, or null if doesn't exist * @throws ExtensionException if there's a problem unpacking the zip file */ public static Properties getPropertiesFromArchive(File file) throws ExtensionException { try (ZipFile zipFile = new ZipFile(file)) { Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries(); while (zipEntries.hasMoreElements()) { ZipArchiveEntry entry = zipEntries.nextElement(); if (entry.getName().endsWith(PROPERTIES_FILE_NAME)) { final InputStream propFile = zipFile.getInputStream(entry); Properties prop = new Properties(); prop.load(propFile); return prop; } } return null; } catch (IOException e) { throw new ExtensionException(e.getMessage(), ExtensionExceptionType.ZIP_ERROR); } }
Example #5
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void hasSameContentShouldThrowWhenEntryHasSameExtraFieldsSizeButDifferentOrder() throws Exception { ZipEntryWithContent expectedEntry = entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT) .addField(new UidExtraField(1L)) .addField(new SizeExtraField(2L)) .build(); ZipEntryWithContent assertedEntry = entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT) .addField(new SizeExtraField(2L)) .addField(new UidExtraField(1L)) .build(); try (ZipFile expectedZipFile = zipFile(destination, expectedEntry); ZipFile assertedZipFile = zipFile(destination2, assertedEntry)) { assertThatThrownBy(() -> assertThatZip(assertedZipFile) .hasSameContentWith(expectedZipFile)) .isInstanceOf(AssertionError.class); } }
Example #6
Source File: ZipRuleIntegrationTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void shouldZipSources() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp(); Path zip = workspace.buildAndReturnOutput("//example:ziptastic"); // Make sure we have the right files and attributes. try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipArchiveEntry cake = zipFile.getEntry("cake.txt"); assertThat(cake, Matchers.notNullValue()); assertFalse(cake.isUnixSymlink()); assertFalse(cake.isDirectory()); ZipArchiveEntry beans = zipFile.getEntry("beans/"); assertThat(beans, Matchers.notNullValue()); assertFalse(beans.isUnixSymlink()); assertTrue(beans.isDirectory()); ZipArchiveEntry cheesy = zipFile.getEntry("beans/cheesy.txt"); assertThat(cheesy, Matchers.notNullValue()); assertFalse(cheesy.isUnixSymlink()); assertFalse(cheesy.isDirectory()); } }
Example #7
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void hasSameContentShouldThrowWhenEntryHasDifferentExtraFieldsSize() throws Exception { ZipEntryWithContent expectedEntry = entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT) .addField(new UidExtraField(1L)) .addField(new UidExtraField(2L)) .build(); ZipEntryWithContent assertedEntry = entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT) .addField(new UidExtraField(1L)) .build(); try (ZipFile expectedZipFile = zipFile(destination, expectedEntry); ZipFile assertedZipFile = zipFile(destination2, assertedEntry)) { assertThatThrownBy(() -> assertThatZip(assertedZipFile) .hasSameContentWith(expectedZipFile)) .isInstanceOf(AssertionError.class); } }
Example #8
Source File: BarFileValidateTest.java From io with Apache License 2.0 | 6 votes |
/** * barファイル内エントリのファイルサイズ上限値を超えた場合に例外が発生すること. */ @Test public void barファイル内エントリのファイルサイズ上限値を超えた場合に例外が発生すること() { TestBarInstaller testBarInstaller = new TestBarInstaller(); URL fileUrl = ClassLoader.getSystemResource("requestData/barInstall/V1_1_2_bar_minimum.bar"); File file = new File(fileUrl.getPath()); try { ZipFile zipFile = new ZipFile(file, "UTF-8"); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); long maxBarEntryFileSize = 0; while (entries.hasMoreElements()) { ZipArchiveEntry zae = entries.nextElement(); if (zae.isDirectory()) { continue; } testBarInstaller.checkBarFileEntrySize(zae, zae.getName(), maxBarEntryFileSize); } fail("Unexpected exception"); } catch (DcCoreException dce) { String code = DcCoreException.BarInstall.BAR_FILE_ENTRY_SIZE_TOO_LARGE.getCode(); assertEquals(code, dce.getCode()); } catch (Exception ex) { fail("Unexpected exception"); } }
Example #9
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void containsOnlyExtraFieldsShouldNotThrowWhenUnexpectedField() throws Exception { try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) { ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME); archiveEntry.addExtraField(EXTRA_FIELD); archiveOutputStream.putArchiveEntry(archiveEntry); IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream); archiveOutputStream.closeArchiveEntry(); archiveOutputStream.finish(); } try (ZipFile zipFile = new ZipFile(destination)) { assertThatCode(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME) .containsExtraFields())) .doesNotThrowAnyException(); } }
Example #10
Source File: ArchiveUtils.java From gradle-golang-plugin with Mozilla Public License 2.0 | 6 votes |
public static void unZip(Path file, Path target) throws IOException { try (final ZipFile zipFile = new ZipFile(file.toFile())) { final Enumeration<ZipArchiveEntry> files = zipFile.getEntriesInPhysicalOrder(); while (files.hasMoreElements()) { final ZipArchiveEntry entry = files.nextElement(); final Path entryFile = target.resolve(REMOVE_LEADING_GO_PATH_PATTERN.matcher(entry.getName()).replaceFirst("")).toAbsolutePath(); if (entry.isDirectory()) { createDirectoriesIfRequired(entryFile); } else { ensureParentOf(entryFile); try (final InputStream is = zipFile.getInputStream(entry)) { try (final OutputStream os = newOutputStream(entryFile)) { copy(is, os); } } } } } }
Example #11
Source File: CompressExample.java From spring-boot with Apache License 2.0 | 6 votes |
/** * 把一个ZIP文件解压到一个指定的目录中 * @param zipfilename ZIP文件抽象地址 * @param outputdir 目录绝对地址 */ public static void unZipToFolder(String zipfilename, String outputdir) throws IOException { File zipfile = new File(zipfilename); if (zipfile.exists()) { outputdir = outputdir + File.separator; FileUtils.forceMkdir(new File(outputdir)); ZipFile zf = new ZipFile(zipfile, "UTF-8"); Enumeration zipArchiveEntrys = zf.getEntries(); while (zipArchiveEntrys.hasMoreElements()) { ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement(); if (zipArchiveEntry.isDirectory()) { FileUtils.forceMkdir(new File(outputdir + zipArchiveEntry.getName() + File.separator)); } else { IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(outputdir + zipArchiveEntry.getName()))); } } } else { throw new IOException("指定的解压文件不存在:\t" + zipfilename); } }
Example #12
Source File: BasicExtractor.java From embedded-rabbitmq with Apache License 2.0 | 6 votes |
private void extractZip(ZipFile zipFile) { Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String fileName = entry.getName(); File outputFile = new File(config.getExtractionFolder(), fileName); if (entry.isDirectory()) { makeDirectory(outputFile); } else { createNewFile(outputFile); try { InputStream inputStream = zipFile.getInputStream(entry); extractFile(inputStream, outputFile, fileName); } catch (IOException e) { throw new ExtractionException("Error extracting file '" + fileName + "' " + "from downloaded file: " + config.getDownloadTarget(), e); } } } }
Example #13
Source File: ZipUtils.java From yes-cart with Apache License 2.0 | 6 votes |
private void unzipEntry(final ZipFile zipfile, final ZipArchiveEntry entry, final File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } try (BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) { IOUtils.copy(inputStream, outputStream); } }
Example #14
Source File: ExtractionTools.java From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 | 6 votes |
private static void extractZipFile(final File destination, final ZipFile zipFile) throws IOException { final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); final File entryDestination = getDestinationFile(destination, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); final InputStream in = zipFile.getInputStream(entry); try (final OutputStream out = new FileOutputStream(entryDestination)) { IOUtils.copy(in, out); IOUtils.closeQuietly(in); } } } }
Example #15
Source File: ZipRuleIntegrationTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void shouldNotMergeSourceJarsIfRequested() throws Exception { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-merge", tmp); workspace.setUp(); Path zip = workspace.buildAndReturnOutput("//example:no-merge"); // Gather expected file names Path sourceJar = workspace.buildAndReturnOutput("//example:cake#src"); Path actualJar = workspace.buildAndReturnOutput("//example:cake"); try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipArchiveEntry item = zipFile.getEntry(sourceJar.getFileName().toString()); assertThat(item, Matchers.notNullValue()); item = zipFile.getEntry(actualJar.getFileName().toString()); assertThat(item, Matchers.notNullValue()); item = zipFile.getEntry("cake.txt"); assertThat(item, Matchers.notNullValue()); } }
Example #16
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void isDirectoryShouldNotThrowWhenDirectory() throws Exception { try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) { ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new Directory("any"), DIRECTORY_NAME); archiveOutputStream.putArchiveEntry(archiveEntry); archiveOutputStream.closeArchiveEntry(); archiveOutputStream.finish(); } try (ZipFile zipFile = new ZipFile(destination)) { assertThatCode(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(DIRECTORY_NAME) .isDirectory())) .doesNotThrowAnyException(); } }
Example #17
Source File: ZipStepTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void willRecurseIntoSubdirectories() throws Exception { 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, Paths.get("zipdir")); assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode()); // Make sure we have the right attributes. try (ZipFile zipFile = new ZipFile(out.toFile())) { ZipArchiveEntry entry = zipFile.getEntry("child/"); assertNotEquals(entry.getUnixMode() & MostFiles.S_IFDIR, 0); } try (ZipArchive zipArchive = new ZipArchive(out, false)) { assertEquals(ImmutableSet.of("file1.txt", "child/file2.txt"), zipArchive.getFileNames()); } }
Example #18
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsExactlyEntriesMatchingShouldThrowWhenWrongOrder() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) { assertThatThrownBy(() -> assertThatZip(zipFile) .containsExactlyEntriesMatching( hasName(ENTRY_NAME_2), hasName(ENTRY_NAME))) .isInstanceOf(AssertionError.class); } }
Example #19
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsExactlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception { try (ZipFile zipFile = buildZipFile()) { assertThatCode(() -> assertThatZip(zipFile) .containsExactlyEntriesMatching()) .doesNotThrowAnyException(); } }
Example #20
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsOnlyEntriesMatchingShouldThrowWhenExpectingLessEntries() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) { assertThatThrownBy(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME))) .isInstanceOf(AssertionError.class); } }
Example #21
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void hasSameContentShouldThrowWhenExpectedZipFileIsNull() throws Exception { try (ZipFile assertedZipFile = zipFile(destination, entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT).build())) { ZipFile expectedZipFile = null; assertThatThrownBy(() -> assertThatZip(assertedZipFile) .hasSameContentWith(expectedZipFile)) .isInstanceOf(AssertionError.class); } }
Example #22
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsOnlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) { assertThatThrownBy(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME), hasName(ENTRY_NAME_2), hasName("extraEntry"))) .isInstanceOf(AssertionError.class); } }
Example #23
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsOnlyEntriesMatchingShouldNotThrowWhenWrongOrder() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) { assertThatCode(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME_2), hasName(ENTRY_NAME))) .doesNotThrowAnyException(); } }
Example #24
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void allSatisfiesShouldNotThrowWhenAllEntriesMatchAssertion() throws Exception { ZipEntryWithContent firstEntry = entryBuilder().name("entry 1").content(ENTRY_CONTENT).build(); ZipEntryWithContent secondEntry = entryBuilder().name("entry 2").content(ENTRY_CONTENT).build(); try (ZipFile assertedZipFile = zipFile(destination, firstEntry, secondEntry)) { assertThatCode(() -> assertThatZip(assertedZipFile) .allSatisfies(entry -> entry.hasStringContent(STRING_ENTRY_CONTENT))) .doesNotThrowAnyException(); } }
Example #25
Source File: ZipRuleIntegrationTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void shouldExcludeOneFileInSubDirectory() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp(); Path zip = workspace.buildAndReturnOutput("//example:excludesexactmatchinsubfolder"); try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipArchiveEntry cake = zipFile.getEntry("cake.txt"); assertThat(cake, Matchers.notNullValue()); ZipArchiveEntry cheesy = zipFile.getEntry("beans/cheesy.txt"); assertThat(cheesy, Matchers.nullValue()); } }
Example #26
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void hasNameShouldThrowWhenWrongName() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY)) { assertThatThrownBy(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME_2))) .isInstanceOf(AssertionError.class); } }
Example #27
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsOnlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) { assertThatCode(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching( hasName(ENTRY_NAME), hasName(ENTRY_NAME_2))) .doesNotThrowAnyException(); } }
Example #28
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void containsOnlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception { try (ZipFile zipFile = buildZipFile()) { assertThatCode(() -> assertThatZip(zipFile) .containsOnlyEntriesMatching()) .doesNotThrowAnyException(); } }
Example #29
Source File: ZipAssertTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void hasNoEntryShouldThrowWhenNotEmpty() throws Exception { try (ZipFile zipFile = buildZipFile(ENTRY)) { assertThatThrownBy(() -> assertThatZip(zipFile) .hasNoEntry()) .isInstanceOf(AssertionError.class); } }
Example #30
Source File: Mq2Xliff.java From translationstudio8 with GNU General Public License v2.0 | 5 votes |
/** * 解析 memoQ 的源文件,并将内容拷贝至骨架文件中 * @param mqZip * @param hsSkeleton R8 hsxliff的骨架文件 * @throws Exception */ private void parseMQZip(String mqZip, String hsSkeleton) throws Exception{ ZipFile zipFile = new ZipFile(new File(mqZip), "utf-8"); Enumeration<?> e = zipFile.getEntries(); byte ch[] = new byte[1024]; String outputFile = ""; File mqSklTempFile = File.createTempFile("tempskl", "skl"); mqSklTempFile.deleteOnExit(); while (e.hasMoreElements()) { ZipArchiveEntry zipEntry = (ZipArchiveEntry) e.nextElement(); if ("document.mqxliff".equals(zipEntry.getName())) { outputFile = hsSkeleton; }else { outputFile = mqSklTempFile.getAbsolutePath(); } File zfile = new File(outputFile); FileOutputStream fouts = new FileOutputStream(zfile); InputStream in = zipFile.getInputStream(zipEntry); int i; while ((i = in.read(ch)) != -1) fouts.write(ch, 0, i); fouts.close(); in.close(); } //解析r8骨加文件,并把 mq 的骨架信息添加到 r8 的骨架文件中 parseHSSkeletonFile(); copyMqSklToHsSkl(mqSklTempFile); }