List of usage examples for org.apache.commons.compress.archivers.zip ZipFile ZipFile
public ZipFile(String name) throws IOException
From source file:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java
/** * Writes a modified version of zip_Source into target. * * @author S3460/*w w w . j a v a2 s .c om*/ * @param zipSource the zip source * @param target the target * @return the zip file * @throws Exception the exception */ private ZipFile makeTargetZipFile(ZipFile zipSource, File target) throws Exception { ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(target)); for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) { ZipArchiveEntry sourceEntry = enumer.nextElement(); out.putArchiveEntry(new ZipArchiveEntry(sourceEntry.getName())); byte[] oldBytes = toBytes(zipSource, sourceEntry); byte[] newBytes = getRandomBytes(); byte[] mixedBytes = mixBytes(oldBytes, newBytes); out.write(mixedBytes, 0, mixedBytes.length); out.flush(); out.closeArchiveEntry(); } out.putArchiveEntry(new ZipArchiveEntry("zipentry" + entryMaxSize + 1)); byte[] bytes = getRandomBytes(); out.write(bytes, 0, bytes.length); out.flush(); out.closeArchiveEntry(); out.putArchiveEntry(new ZipArchiveEntry("zipentry" + (entryMaxSize + 2))); out.closeArchiveEntry(); out.flush(); out.finish(); out.close(); return new ZipFile(targetFile); }
From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java
@Test public void shouldExcludeNothingInSubDirectory() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp();/* ww w.j a va 2 s . co m*/ Path zip = workspace.buildAndReturnOutput("//example:excludesnothinginsubfolder"); 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.notNullValue()); } }
From source file:fr.acxio.tools.agia.tasks.ZipFilesTaskletTest.java
@Test public void testZipDirectory() throws Exception { FileUtils.forceMkdir(new File("target/Z-testfiles/source")); FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP0-input.csv")); FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP1-input.csv")); String aTargetFilename = "target/Z2-input.zip"; ZipFilesTasklet aTasklet = new ZipFilesTasklet(); aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/")); FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory(); aSourceFactory.setPattern("file:target/Z-testfiles/source/"); aTasklet.setSourceFactory(aSourceFactory); ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory(); aDestinationFactory.setExpression(aTargetFilename); aTasklet.setDestinationFactory(aDestinationFactory); assertFalse(new File(aTargetFilename).exists()); StepContribution aStepContribution = mock(StepContribution.class); assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null)); verify(aStepContribution, times(3)).incrementReadCount(); verify(aStepContribution, times(3)).incrementWriteCount(1); assertTrue(new File(aTargetFilename).exists()); ZipFile aZipFile = new ZipFile(new File(aTargetFilename)); Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries(); assertTrue(aEntries.hasMoreElements()); assertEquals("source", aEntries.nextElement().getName()); assertTrue(aEntries.hasMoreElements()); assertEquals("source/CP0-input.csv", aEntries.nextElement().getName()); assertTrue(aEntries.hasMoreElements()); assertEquals("source/CP1-input.csv", aEntries.nextElement().getName()); assertFalse(aEntries.hasMoreElements()); aZipFile.close();//from ww w . j a v a 2 s . c o m }
From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java
@Test public void shouldExcludeOneFileInSubDirectory() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp();/* ww w .ja va 2s . c om*/ 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()); } }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
/** * Extracts a {@code *.zip} archive into the target folder. * @param monitor the progress monitor/*w w w . j a va 2 s .co m*/ * @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:de.catma.ui.repository.wizard.FileTypePanel.java
private ArrayList<SourceDocumentResult> makeSourceDocumentResultsFromInputFile(TechInfoSet inputTechInfoSet) throws MalformedURLException, IOException { ArrayList<SourceDocumentResult> output = new ArrayList<SourceDocumentResult>(); FileType inputFileType = FileType.getFileType(inputTechInfoSet.getMimeType()); if (inputFileType != FileType.ZIP) { SourceDocumentResult outputSourceDocumentResult = new SourceDocumentResult(); String sourceDocumentID = repository.getIdFromURI(inputTechInfoSet.getURI()); outputSourceDocumentResult.setSourceDocumentID(sourceDocumentID); SourceDocumentInfo outputSourceDocumentInfo = outputSourceDocumentResult.getSourceDocumentInfo(); outputSourceDocumentInfo.setTechInfoSet(new TechInfoSet(inputTechInfoSet)); outputSourceDocumentInfo.setContentInfoSet(new ContentInfoSet()); outputSourceDocumentInfo.getTechInfoSet().setFileType(inputFileType); output.add(outputSourceDocumentResult); } else { //TODO: put this somewhere sensible URI uri = inputTechInfoSet.getURI(); ZipFile zipFile = new ZipFile(uri.getPath()); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); String tempDir = ((CatmaApplication) UI.getCurrent()).getTempDirectory(); IDGenerator idGenerator = new IDGenerator(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String fileName = FilenameUtils.getName(entry.getName()); String fileId = idGenerator.generate(); File entryDestination = new File(tempDir, fileId); if (entryDestination.exists()) { entryDestination.delete(); }// w ww . ja v a 2 s . c om entryDestination.getParentFile().mkdirs(); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryDestination)); IOUtils.copy(bis, bos); IOUtils.closeQuietly(bis); IOUtils.closeQuietly(bos); SourceDocumentResult outputSourceDocumentResult = new SourceDocumentResult(); URI newURI = entryDestination.toURI(); String repositoryId = repository.getIdFromURI(newURI); // we need to do this as a catma:// is appended outputSourceDocumentResult.setSourceDocumentID(repositoryId); SourceDocumentInfo outputSourceDocumentInfo = outputSourceDocumentResult .getSourceDocumentInfo(); TechInfoSet newTechInfoSet = new TechInfoSet(fileName, null, newURI); // TODO: MimeType detection ? FileType newFileType = FileType.getFileTypeFromName(fileName); newTechInfoSet.setFileType(newFileType); outputSourceDocumentInfo.setTechInfoSet(newTechInfoSet); outputSourceDocumentInfo.setContentInfoSet(new ContentInfoSet()); output.add(outputSourceDocumentResult); } } ZipFile.closeQuietly(zipFile); } for (SourceDocumentResult sdr : output) { TechInfoSet sdrTechInfoSet = sdr.getSourceDocumentInfo().getTechInfoSet(); String sdrSourceDocumentId = sdr.getSourceDocumentID(); ProtocolHandler protocolHandler = getProtocolHandlerForUri(sdrTechInfoSet.getURI(), sdrSourceDocumentId, sdrTechInfoSet.getMimeType()); String mimeType = protocolHandler.getMimeType(); sdrTechInfoSet.setMimeType(mimeType); FileType sdrFileType = FileType.getFileType(mimeType); sdrTechInfoSet.setFileType(sdrFileType); if (sdrFileType.isCharsetSupported()) { Charset charset = Charset.forName(protocolHandler.getEncoding()); sdrTechInfoSet.setCharset(charset); } else { sdrTechInfoSet.setFileOSType(FileOSType.INDEPENDENT); } loadSourceDocumentAndContent(sdr); } return output; }
From source file:com.vividsolutions.jump.io.CompressedFile.java
/** * Utility file open function - handles compressed and un-compressed files. * /*from w w w . j a v a 2s . c om*/ * @param filePath * name of the file to search for. * @param compressedEntry * name of the compressed file. * * <p> * If compressedEntry = null, opens a FileInputStream on filePath * </p> * * <p> * If filePath ends in ".zip" - opens the compressed Zip and * looks for the file called compressedEntry * </p> * * <p> * If filePath ends in ".gz" - opens the compressed .gz file. * </p> */ public static InputStream openFile(String filePath, String compressedEntry) throws IOException { System.out.println(filePath + " extract " + compressedEntry); if (isTar(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); if (filePath.toLowerCase().endsWith("gz")) is = new GzipCompressorInputStream(is, true); else if (filePath.matches("(?i).*bz2?")) is = new BZip2CompressorInputStream(is, true); else if (filePath.matches("(?i).*xz")) is = new XZCompressorInputStream(is, true); TarArchiveInputStream tis = new TarArchiveInputStream(is); if (compressedEntry == null) return is; TarArchiveEntry entry; while ((entry = tis.getNextTarEntry()) != null) { if (entry.getName().equals(compressedEntry)) return tis; } throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry == null && isGZip(filePath)) { // gz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new GzipCompressorInputStream(is, true); } else if (compressedEntry == null && isBZip(filePath)) { // bz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new BZip2CompressorInputStream(is, true); //return new org.itadaki.bzip2.BZip2InputStream(is, false); } else if (compressedEntry == null && isXZ(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new XZCompressorInputStream(is, true); } else if (compressedEntry != null && isZip(filePath)) { ZipFile zipFile = new ZipFile(filePath); ZipArchiveEntry zipEntry = zipFile.getEntry(compressedEntry); if (zipEntry != null) return zipFile.getInputStream(zipEntry); throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry != null && isSevenZ(filePath)) { SevenZFileGiveStream sevenZFile = new SevenZFileGiveStream(new File(filePath)); SevenZArchiveEntry entry; while ((entry = sevenZFile.getNextEntry()) != null) { if (entry.getName().equals(compressedEntry)) return sevenZFile.getCurrentEntryInputStream(); } throw createArchiveFNFE(filePath, compressedEntry); } // return plain stream if no compressedEntry else if (compressedEntry == null) { return new FileInputStream(filePath); } else { throw new IOException("Couldn't determine compressed file type for file '" + filePath + "' supposedly containing '" + compressedEntry + "'."); } }
From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java
@Test public void shouldExcludeOnlyCake() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp();/*w ww . j a v a 2 s. c o m*/ Path zip = workspace.buildAndReturnOutput("//example:excludecake"); try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipArchiveEntry cake = zipFile.getEntry("cake.txt"); assertThat(cake, Matchers.nullValue()); ZipArchiveEntry taco = zipFile.getEntry("menu.txt"); assertThat(taco, Matchers.notNullValue()); } }
From source file:at.spardat.xma.xdelta.JarDelta.java
/** * Compute delta.// w ww. j a v a 2 s .c o m * * @param source the source * @param target the target * @param output the output * @param list the list * @param prefix the prefix * @throws IOException Signals that an I/O exception has occurred. */ public void computeDelta(ZipFile source, ZipFile target, ZipArchiveOutputStream output, PrintWriter list, String prefix) throws IOException { try { for (Enumeration<ZipArchiveEntry> enumer = target.getEntries(); enumer.hasMoreElements();) { calculatedDelta = null; ZipArchiveEntry targetEntry = enumer.nextElement(); ZipArchiveEntry sourceEntry = findBestSource(source, target, targetEntry); String nextEntryName = prefix + targetEntry.getName(); if (sourceEntry != null && zipFilesPattern.matcher(sourceEntry.getName()).matches() && !equal(sourceEntry, targetEntry)) { nextEntryName += "!"; } nextEntryName += "|" + Long.toHexString(targetEntry.getCrc()); if (sourceEntry != null) { nextEntryName += ":" + Long.toHexString(sourceEntry.getCrc()); } else { nextEntryName += ":0"; } list.println(nextEntryName); if (targetEntry.isDirectory()) { if (sourceEntry == null) { ZipArchiveEntry outputEntry = entryToNewName(targetEntry, prefix + targetEntry.getName()); output.putArchiveEntry(outputEntry); output.closeArchiveEntry(); } } else { if (sourceEntry == null || sourceEntry.getSize() <= Delta.DEFAULT_CHUNK_SIZE || targetEntry.getSize() <= Delta.DEFAULT_CHUNK_SIZE) { // new Entry od. alter Eintrag od. neuer Eintrag leer ZipArchiveEntry outputEntry = entryToNewName(targetEntry, prefix + targetEntry.getName()); output.putArchiveEntry(outputEntry); try (InputStream in = target.getInputStream(targetEntry)) { int read = 0; while (-1 < (read = in.read(buffer))) { output.write(buffer, 0, read); } output.flush(); } output.closeArchiveEntry(); } else { if (!equal(sourceEntry, targetEntry)) { if (zipFilesPattern.matcher(sourceEntry.getName()).matches()) { File embeddedTarget = File.createTempFile("jardelta-tmp", ".zip"); File embeddedSource = File.createTempFile("jardelta-tmp", ".zip"); try (FileOutputStream out = new FileOutputStream(embeddedSource); InputStream in = source.getInputStream(sourceEntry); FileOutputStream out2 = new FileOutputStream(embeddedTarget); InputStream in2 = target.getInputStream(targetEntry)) { int read = 0; while (-1 < (read = in.read(buffer))) { out.write(buffer, 0, read); } out.flush(); read = 0; while (-1 < (read = in2.read(buffer))) { out2.write(buffer, 0, read); } out2.flush(); computeDelta(new ZipFile(embeddedSource), new ZipFile(embeddedTarget), output, list, prefix + sourceEntry.getName() + "!"); } finally { embeddedSource.delete(); embeddedTarget.delete(); } } else { ZipArchiveEntry outputEntry = new ZipArchiveEntry( prefix + targetEntry.getName() + ".gdiff"); outputEntry.setTime(targetEntry.getTime()); outputEntry.setComment("" + targetEntry.getCrc()); output.putArchiveEntry(outputEntry); if (calculatedDelta != null) { output.write(calculatedDelta); output.flush(); } else { try (ByteArrayOutputStream outbytes = new ByteArrayOutputStream()) { Delta d = new Delta(); DiffWriter diffWriter = new GDiffWriter(new DataOutputStream(outbytes)); int sourceSize = (int) sourceEntry.getSize(); byte[] sourceBytes = new byte[sourceSize]; try (InputStream sourceStream = source.getInputStream(sourceEntry)) { for (int erg = sourceStream.read( sourceBytes); erg < sourceBytes.length; erg += sourceStream .read(sourceBytes, erg, sourceBytes.length - erg)) ; } d.compute(sourceBytes, target.getInputStream(targetEntry), diffWriter); output.write(outbytes.toByteArray()); } } output.closeArchiveEntry(); } } } } } } finally { source.close(); target.close(); } }
From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java
@Test public void shouldUnpackContentsOfZipSources() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp); workspace.setUp();// w w w.j a va2s . c om 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()); } }