List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName
public String getName()
From source file:ezbake.helpers.cdh.Cdh2EzProperties.java
public Configuration getConfiguration(InputStreamDataSource configStream) throws IOException { Configuration configuration = new Configuration(false); try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(configStream.getInputStream())) { ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry(); while (zipEntry != null) { String name = zipEntry.getName(); if (name.endsWith("core-site.xml") || name.endsWith("hdfs-site.xml")) { if (verbose) System.err.println("Reading \"" + name + "\" into Configuration."); ByteArrayOutputStream boas = new ByteArrayOutputStream(); IOUtils.copy(zipInputStream, boas); configuration.addResource(new ByteArrayInputStream(boas.toByteArray()), name); }/*from ww w . jav a 2 s . c o m*/ zipEntry = zipInputStream.getNextZipEntry(); } } return configuration; }
From source file:com.android.tradefed.util.Bugreport.java
/** * Returns the list of files contained inside the zipped bugreport. Null if it's not a zipped * bugreport.//from w ww . j av a2 s . com */ public List<String> getListOfFiles() { if (mBugreport == null) { return null; } List<String> list = new ArrayList<>(); if (!mIsZipped) { return null; } try (ZipFile zipBugreport = new ZipFile(mBugreport)) { for (ZipArchiveEntry entry : Collections.list(zipBugreport.getEntries())) { list.add(entry.getName()); } } catch (IOException e) { CLog.e("Error reading the list of files in the bugreport"); CLog.e(e); } return list; }
From source file:com.google.gerrit.acceptance.ssh.UploadArchiveIT.java
@Test public void zipFormat() throws Exception { PushOneCommit.Result r = createChange(); String abbreviated = r.getCommit().abbreviate(8).name(); String c = command(r, abbreviated); InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c)); // Wrap with PacketLineIn to read ACK bytes from output stream PacketLineIn in = new PacketLineIn(out); String tmp = in.readString(); assertThat(tmp).isEqualTo("ACK"); tmp = in.readString();// ww w. j av a2s. c o m // Skip length (4 bytes) + 1 byte // to position the output stream to the raw zip stream byte[] buffer = new byte[5]; IO.readFully(out, buffer, 0, 5); Set<String> entryNames = new TreeSet<>(); try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) { ZipArchiveEntry zipEntry = zip.getNextZipEntry(); while (zipEntry != null) { String name = zipEntry.getName(); entryNames.add(name); zipEntry = zip.getNextZipEntry(); } } assertThat(entryNames.size()).isEqualTo(1); assertThat(Iterables.getOnlyElement(entryNames)) .isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME)); }
From source file:net.orpiske.ssps.common.archive.zip.ZipArchive.java
/** * Decompress a file/*from w w w . java 2s . com*/ * @param source the source file to be uncompressed * @param destination the destination directory * @return the number of bytes read * @throws IOException for lower level I/O errors */ public long unpack(File source, File destination) throws IOException { ZipFile zipFile = null; long ret = 0; try { zipFile = new ZipFile(source); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File outFile = new File(destination.getPath() + File.separator + entry.getName()); CompressedArchiveUtils.prepareDestination(outFile); if (!entry.isDirectory()) { ret += extractFile(zipFile, entry, outFile); } int mode = entry.getUnixMode(); PermissionsUtils.setPermissions(mode, outFile); } } finally { if (zipFile != null) { zipFile.close(); } } return ret; }
From source file:io.wcm.maven.plugins.contentpackage.unpacker.ContentUnpacker.java
/** * Unpacks file/*from w w w. j av a2 s .c om*/ * @param file File * @param outputDirectory Output directory * @throws MojoExecutionException */ public void unpack(File file, File outputDirectory) throws MojoExecutionException { ZipFile zipFile = null; try { zipFile = new ZipFile(file); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); if (!exclude(entry.getName(), excludeFiles)) { unpackEntry(zipFile, entry, outputDirectory); } } } catch (IOException ex) { throw new MojoExecutionException("Error reading content package " + file.getAbsolutePath(), ex); } finally { IOUtils.closeQuietly(zipFile); } }
From source file:adams.core.io.ZipUtils.java
/** * Unzips the specified file from a ZIP file. * * @param input the ZIP file to unzip//from ww w. j a v a2 s. co m * @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:io.github.runassudo.gtfs.ZipStreamGTFSFile.java
public FlatGTFSFile toFlatFile() throws IOException { File destBase = new File( new File(GTFSCollection.cacheDir, Hashing.sha256().hashString(parentFile.getName(), StandardCharsets.UTF_8).toString()), Hashing.sha256().hashString(zipEntry.getName(), StandardCharsets.UTF_8).toString()); if (!destBase.exists()) { ZipArchiveInputStream gtfsStream = new ZipArchiveInputStream(parentFile.getInputStream(zipEntry)); ZipArchiveEntry contentEntry; while ((contentEntry = gtfsStream.getNextZipEntry()) != null) { // Copy this file to cache File dest = new File(destBase, contentEntry.getName()); dest.getParentFile().mkdirs(); FileOutputStream os = new FileOutputStream(dest); byte[] buf = new byte[4096]; int len; while ((len = gtfsStream.read(buf)) > 0) { os.write(buf, 0, len);//from w w w. j a v a 2 s. c o m } os.close(); } gtfsStream.close(); } return new FlatGTFSFile(destBase); }
From source file:info.magnolia.ui.framework.command.ImportZipCommand.java
private String extractEntryPath(ZipArchiveEntry entry) { String entryName = entry.getName(); String path = (StringUtils.contains(entryName, "/")) ? StringUtils.substringBeforeLast(entryName, "/") : "/"; if (!path.startsWith("/")) { path = "/" + path; }/*from ww w . j av a 2s .co m*/ // make proper name, the path was already created path = StringUtils.replace(path, "/", BACKSLASH_DUMMY); path = Path.getValidatedLabel(path); path = StringUtils.replace(path, BACKSLASH_DUMMY, "/"); return path; }
From source file:com.nabla.wapp.report.server.ReportZipFile.java
public ZipArchiveEntry getReportDesign() { for (Enumeration<ZipArchiveEntry> iter = impl.getEntries(); iter.hasMoreElements();) { final ZipArchiveEntry entry = iter.nextElement(); if (!entry.isDirectory() && impl.canReadEntryData(entry) && FilenameUtils.isExtension(entry.getName(), ReportManager.REPORT_FILE_EXTENSION)) return entry; }/*w ww . j a v a 2 s .c o m*/ return null; }
From source file:info.magnolia.ui.framework.command.ImportZipCommand.java
private void processEntry(ZipFile zip, ZipArchiveEntry entry) throws IOException, RepositoryException { if (entry.getName().startsWith("__MACOSX")) { return;// w w w .j av a 2s . c o m } else if (entry.getName().endsWith(".DS_Store")) { return; } if (entry.isDirectory()) { ensureFolder(entry); } else { handleFileEntry(zip, entry); } }