List of usage examples for org.apache.commons.compress.archivers.zip ZipFile ZipFile
public ZipFile(String name) throws IOException
From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java
@Test public void testShouldIncludeOutputsContainedInBuckOutOfOtherCells() throws IOException { assumeTrue(Platform.detect() != Platform.WINDOWS); ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenarioWithoutDefaultCell(this, "zip-crosscell", tmp); workspace.setUp();//from w w w.j a va 2s. c om Path childRepoRoot = workspace.getPath("parent/child"); ProcessResult buildResult = workspace.runBuckCommand(childRepoRoot, "build", "--show-output", "//:exported-zip"); buildResult.assertSuccess(); String outputRelpathString = workspace.parseShowOutputStdoutAsStrings(buildResult.getStdout()) .get("//:exported-zip"); Path zip = workspace.getPath("parent/child/" + outputRelpathString); try (ZipFile zipFile = new ZipFile(zip.toFile())) { ZipInspector inspector = new ZipInspector(zip); assertThat(inspector.getZipFileEntries().size(), greaterThan(0)); } }
From source file:com.taobao.android.utils.ZipUtils.java
/** * <p>//from w w w .j ava 2 s .com * unzip. * </p> * * @param zipFile a {@link File} object. * @param destination a {@link String} object. * @param encoding a {@link String} object. * @return a {@link List} object. */ public static List<String> unzip(final File zipFile, final String destination, String encoding) { List<String> fileNames = new ArrayList<String>(); String dest = destination; if (!destination.endsWith("/")) { dest = destination + "/"; } ZipFile file; try { file = null; if (null == encoding) file = new ZipFile(zipFile); else file = new ZipFile(zipFile, encoding); Enumeration<ZipArchiveEntry> en = file.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); File f = new File(dest, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); InputStream is = file.getInputStream(ze); OutputStream os = new FileOutputStream(f); IOUtils.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); } } file.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return fileNames; }
From source file:fr.acxio.tools.agia.tasks.ZipFilesTaskletTest.java
@Test public void testEmptyBaseDirRelativeDir() throws Exception { String aTargetFilename = "target/Z6-input.zip"; ZipFilesTasklet aTasklet = new ZipFilesTasklet(); aTasklet.setSourceBaseDirectory(new FileSystemResource("")); FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory(); aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv"); aTasklet.setSourceFactory(aSourceFactory); ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory(); aDestinationFactory.setExpression(aTargetFilename); aTasklet.setDestinationFactory(aDestinationFactory); assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null)); assertTrue(new File(aTargetFilename).exists()); ZipFile aZipFile = new ZipFile(new File(aTargetFilename)); Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries(); assertTrue(aEntries.hasMoreElements()); assertEquals("src/test/resources/testFiles/input.csv", aEntries.nextElement().getName()); assertFalse(aEntries.hasMoreElements()); aZipFile.close();// w w w . j ava 2s . c o m }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * <p>/*from www .ja v a 2 s. c o m*/ * unzip. * </p> * * @param zipFile a {@link java.io.File} object. * @param destination a {@link String} object. * @param encoding a {@link String} object. * @return a {@link java.util.List} object. */ public static List<String> unzip(final File zipFile, final String destination, String encoding) { List<String> fileNames = new ArrayList<String>(); String dest = destination; if (!destination.endsWith(File.separator)) { dest = destination + File.separator; } ZipFile file; try { file = null; if (null == encoding) { file = new ZipFile(zipFile); } else { file = new ZipFile(zipFile, encoding); } Enumeration<ZipArchiveEntry> en = file.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); File f = new File(dest, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); InputStream is = file.getInputStream(ze); OutputStream os = new FileOutputStream(f); IOUtils.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); } } file.close(); } catch (IOException e) { e.printStackTrace(); } return fileNames; }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * <p>/*from w ww . j a va2 s. c o m*/ * isZipFile. * </p> * * @param zipFile a {@link java.io.File} object. * @return a boolean. */ public static boolean isZipFile(File zipFile) { try { ZipFile zf = new ZipFile(zipFile); boolean isZip = zf.getEntries().hasMoreElements(); zf.close(); return isZip; } catch (IOException e) { return false; } }
From source file:adams.core.io.ZipUtils.java
/** * Unzips the specified file from a ZIP file. * * @param input the ZIP file to unzip/* w w w. ja v a2 s. c om*/ * @param archiveFile the file from the archive to extract * @param output the name of the output file * @param createDirs whether to create the directory structure represented * by output file * @param bufferSize the buffer size to use * @param errors for storing potential errors * @return whether file was successfully extracted */ public static boolean decompress(File input, String archiveFile, File output, boolean createDirs, int bufferSize, StringBuilder errors) { boolean result; ZipFile zipfile; Enumeration<ZipArchiveEntry> enm; ZipArchiveEntry entry; File outFile; String outName; byte[] buffer; BufferedInputStream in; BufferedOutputStream out; FileOutputStream fos; int len; String error; long read; result = false; zipfile = null; try { // unzip archive buffer = new byte[bufferSize]; zipfile = new ZipFile(input.getAbsoluteFile()); enm = zipfile.getEntries(); while (enm.hasMoreElements()) { entry = enm.nextElement(); if (entry.isDirectory()) continue; if (!entry.getName().equals(archiveFile)) continue; in = null; out = null; fos = null; outName = null; try { // output name outName = output.getAbsolutePath(); // create directory, if necessary outFile = new File(outName).getParentFile(); if (!outFile.exists()) { if (!createDirs) { error = "Output directory '" + outFile.getAbsolutePath() + " does not exist', " + "skipping extraction of '" + outName + "'!"; System.err.println(error); errors.append(error + "\n"); break; } else { if (!outFile.mkdirs()) { error = "Failed to create directory '" + outFile.getAbsolutePath() + "', " + "skipping extraction of '" + outName + "'!"; System.err.println(error); errors.append(error + "\n"); break; } } } // extract data in = new BufferedInputStream(zipfile.getInputStream(entry)); fos = new FileOutputStream(outName); out = new BufferedOutputStream(fos, bufferSize); read = 0; while (read < entry.getSize()) { len = in.read(buffer); read += len; out.write(buffer, 0, len); } result = true; break; } catch (Exception e) { result = false; error = "Error extracting '" + entry.getName() + "' to '" + outName + "': " + e; System.err.println(error); errors.append(error + "\n"); } finally { FileUtils.closeQuietly(in); FileUtils.closeQuietly(out); FileUtils.closeQuietly(fos); } } } catch (Exception e) { result = false; e.printStackTrace(); errors.append("Error occurred: " + e + "\n"); } finally { if (zipfile != null) { try { zipfile.close(); } catch (Exception e) { // ignored } } } return result; }
From source file:com.taobao.android.utils.ZipUtils.java
/** * zip?/* w w w. j a v a2 s .c om*/ * * @param zipFile * @param path * @param destFolder * @throws IOException */ public static File extractZipFileToFolder(File zipFile, String path, File destFolder) { ZipFile zip; File destFile = null; try { zip = new ZipFile(zipFile); ZipArchiveEntry zipArchiveEntry = zip.getEntry(path); if (null != zipArchiveEntry) { String name = zipArchiveEntry.getName(); name = FilenameUtils.getName(name); destFile = new File(destFolder, name); destFolder.mkdirs(); destFile.createNewFile(); InputStream is = zip.getInputStream(zipArchiveEntry); FileOutputStream fos = new FileOutputStream(destFile); int length = 0; byte[] b = new byte[1024]; while ((length = is.read(b, 0, 1024)) != -1) { fos.write(b, 0, length); } is.close(); fos.close(); } if (null != zip) ZipFile.closeQuietly(zip); } catch (IOException e) { e.printStackTrace(); } return destFile; }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static List<String> listZipEntries(File zipFile) { List<String> list = new ArrayList<String>(); ZipFile zip;/*from w ww . ja v a 2 s . c om*/ try { zip = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> en = zip.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); String name = ze.getName(); name = StringUtils.replace(name, "/", "."); if (name.endsWith(".class")) { list.add(name); } } if (null != zip) { ZipFile.closeQuietly(zip); } } catch (IOException e) { } return list; }
From source file:autoupdater.DownloadLatestZipFromRepo.java
/** * Aggregation method for downloading and unzipping. * * @param jarURL the url of the version specific location * @param fileDAO which fileDAO implementation that should be used * @param isWindows if true, the OS will assumed to be windows * download//from w ww . ja v a 2 s . c o m * @return the downloaded {@code MavenJarFile} * @throws MalformedURLException * @throws IOException * @throws XMLStreamException */ private static MavenJarFile downloadAndUnzipJar(final File downloadFolder, final String artifactId, URL jarURL, FileDAO fileDAO, boolean cleanupZipFile, boolean isWindows) throws MalformedURLException, IOException, XMLStreamException { if (isWindows) { fileDAO.unzipFile(new ZipFile(downloadedFile), downloadFolder.getParentFile()); } else { fileDAO.unGzipAndUntarFile(new GZIPInputStream(jarURL.openStream()), downloadedFile); } // get the new jar file MavenJarFile newMavenJar; if (isWindows) { newMavenJar = fileDAO.getMavenJarFileFromFolderWithArtifactId(downloadFolder, artifactId); } else { newMavenJar = fileDAO.getMavenJarFileFromFolderWithArtifactId(downloadFolder, artifactId); } // delete the downloaded zip file if (cleanupZipFile) { if (!downloadedFile.delete()) { throw new IOException("could not delete the zip file"); } } return newMavenJar; }
From source file:com.facebook.buck.io.ProjectFilesystemTest.java
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. Path exe = tmp.newFile("test.exe"); MoreFiles.makeExecutable(exe);//from ww w . ja va 2 s . c o m // Archive it into a zipfile using `ProjectFileSystem.createZip`. Path zipFile = tmp.getRoot().resolve("test.zip"); filesystem.createZip(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()); } }