Java Code Examples for org.apache.commons.compress.archivers.ArchiveEntry#getName()
The following examples show how to use
org.apache.commons.compress.archivers.ArchiveEntry#getName() .
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: NpmPackageParser.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
/** * Determines if the specified archive entry's name could represent a valid package.json file. Typically these would * be under {@code package/package.json}, but there are tarballs out there that have the package.json in a different * directory. */ @VisibleForTesting boolean isPackageJson(final ArchiveEntry entry) { if (entry.isDirectory()) { return false; } String name = entry.getName(); if (name == null) { return false; } else if (!name.endsWith(PACKAGE_JSON_SUBPATH)) { return false; // not a package.json file } else if (name.startsWith(PACKAGE_JSON_SUBPATH)) { return false; // should not be at the root, should be under the path containing the actual package, whatever it is } else { return name.indexOf(PACKAGE_JSON_SUBPATH) == name.indexOf(SEPARATOR); // path should only be one directory deep } }
Example 2
Source File: ArchiveUtils.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * Extract a single file from a tar.gz file. Does not support directories. * NOTE: This should not be used for batch extraction of files, due to the need to iterate over the entries until the * specified entry is found. Use {@link #unzipFileTo(String, String)} for batch extraction instead * * @param tarGz A tar.gz file * @param destination The destination file to extract to * @param pathInTarGz The path in the tar.gz file to extract */ public static void tarGzExtractSingleFile(File tarGz, File destination, String pathInTarGz) throws IOException { try(TarArchiveInputStream tin = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarGz))))) { ArchiveEntry entry; boolean extracted = false; while((entry = tin.getNextTarEntry()) != null){ String name = entry.getName(); if(pathInTarGz.equals(name)){ try(OutputStream os = new BufferedOutputStream(new FileOutputStream(destination))){ IOUtils.copy(tin, os); } extracted = true; } } Preconditions.checkState(extracted, "No file was extracted. File not found? %s", pathInTarGz); } }
Example 3
Source File: Tar.java From writelatex-git-bridge with MIT License | 6 votes |
public static void untar( InputStream tar, File parentDir ) throws IOException { TarArchiveInputStream tin = new TarArchiveInputStream(tar); ArchiveEntry e; while ((e = tin.getNextEntry()) != null) { File f = new File(parentDir, e.getName()); f.setLastModified(e.getLastModifiedDate().getTime()); f.getParentFile().mkdirs(); if (e.isDirectory()) { f.mkdir(); continue; } long size = e.getSize(); checkFileSize(size); try (OutputStream out = new FileOutputStream(f)) { /* TarInputStream pretends each entry's EOF is the stream's EOF */ IOUtils.copy(tin, out); } } }
Example 4
Source File: IndexController.java From config-toolkit with Apache License 2.0 | 6 votes |
@PostMapping("/import/{version:.+}") public ModelAndView importData(@PathVariable String version, MultipartFile file){ final String fileName = file.getOriginalFilename(); LOGGER.info("Upload file : {}", fileName); try (InputStream in = file.getInputStream()) { if (fileName.endsWith(PROPERTIES)) { saveGroup(version, fileName, in); } else if (fileName.endsWith(ZIP)) { try (ZipArchiveInputStream input = new ZipArchiveInputStream(in)) { ArchiveEntry nextEntry = null; while ((nextEntry = input.getNextEntry()) != null) { String entryName = nextEntry.getName(); saveGroup(version, entryName, input); } } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return new ModelAndView("redirect:/version/" + version); }
Example 5
Source File: P4ExtFileUtils.java From p4ic4idea with Apache License 2.0 | 6 votes |
private static void extractArchive(ArchiveInputStream archiveInputStream, File outputDir) throws IOException { createDirectory(outputDir); try { ArchiveEntry entry = archiveInputStream.getNextEntry(); while (entry != null) { File node = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!node.mkdirs() && !node.isDirectory()) { throw new IOException("Could not create directory " + node); } } else { extractFile(archiveInputStream, node); } entry = archiveInputStream.getNextEntry(); } } finally { archiveInputStream.close(); } }
Example 6
Source File: TarContainerPacker.java From hadoop-ozone with Apache License 2.0 | 6 votes |
@Override public byte[] unpackContainerDescriptor(InputStream input) throws IOException { try (InputStream decompressed = decompress(input); ArchiveInputStream archiveInput = untar(decompressed)) { ArchiveEntry entry = archiveInput.getNextEntry(); while (entry != null) { String name = entry.getName(); if (CONTAINER_FILE_NAME.equals(name)) { return readEntry(archiveInput, entry.getSize()); } entry = archiveInput.getNextEntry(); } } catch (CompressorException e) { throw new IOException( "Can't read the container descriptor from the container archive", e); } throw new IOException( "Container descriptor is missing from the container archive."); }
Example 7
Source File: Utils.java From tutorials with MIT License | 6 votes |
/** * Extract an entry of the input stream into a given folder * @param entry * @param tar * @param folder * @throws IOException */ public static void extractEntry(ArchiveEntry entry, InputStream tar, String folder) throws IOException { final int bufferSize = 4096; final String path = folder + entry.getName(); if (entry.isDirectory()) { new File(path).mkdirs(); } else { int count; byte[] data = new byte[bufferSize]; // @formatter:off try (FileOutputStream os = new FileOutputStream(path); BufferedOutputStream dest = new BufferedOutputStream(os, bufferSize)) { // @formatter:off while ((count = tar.read(data, 0, bufferSize)) != -1) { dest.write(data, 0, count); } } } }
Example 8
Source File: PressUtility.java From jstarcraft-core with Apache License 2.0 | 6 votes |
public static void decompressZip(File fromFile, File toDirectory) { try (FileInputStream fileInputStream = new FileInputStream(fromFile); ZipArchiveInputStream archiveInputStream = new ZipArchiveInputStream(fileInputStream)) { byte[] buffer = new byte[BUFFER_SIZE]; ArchiveEntry archiveEntry; while (null != (archiveEntry = archiveInputStream.getNextEntry())) { File file = new File(toDirectory, archiveEntry.getName()); try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { int length = -1; while ((length = archiveInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, length); } fileOutputStream.flush(); } } } catch (IOException exception) { throw new IllegalStateException("解压ZIP异常:", exception); } }
Example 9
Source File: UnpackBuilder.java From kite with Apache License 2.0 | 6 votes |
private boolean parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedExtractor extractor, Record record) { String name = entry.getName(); if (archive.canReadEntryData(entry)) { Record entrydata = new Record(); // TODO: or pass myself? //Record entrydata = record.copy(); // For detectors to work, we need a mark/reset supporting // InputStream, which ArchiveInputStream isn't, so wrap TemporaryResources tmp = new TemporaryResources(); try { TikaInputStream tis = TikaInputStream.get(archive, tmp); return extractor.parseEmbedded(tis, entrydata, name, getChild()); } finally { try { tmp.dispose(); } catch (TikaException e) { LOG.warn("Cannot dispose of tmp Tika resources", e); } } } else { return false; } }
Example 10
Source File: ArchiveUtils.java From konduit-serving with Apache License 2.0 | 6 votes |
protected static List<String> tarGzListFiles(File file, boolean isTarGz) throws IOException { try(TarArchiveInputStream tin = isTarGz ? new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) : new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(file)))) { ArchiveEntry entry; List<String> out = new ArrayList<>(); while((entry = tin.getNextTarEntry()) != null){ String name = entry.getName(); out.add(name); } return out; } }
Example 11
Source File: CompressedFileReference.java From vespa with Apache License 2.0 | 5 votes |
private static void decompress(ArchiveInputStream archiveInputStream, File outputFile) throws IOException { int entries = 0; ArchiveEntry entry; while ((entry = archiveInputStream.getNextEntry()) != null) { File outFile = new File(outputFile, entry.getName()); if (entry.isDirectory()) { if (!(outFile.exists() && outFile.isDirectory())) { log.log(Level.FINE, () -> "Creating dir: " + outFile.getAbsolutePath()); if (!outFile.mkdirs()) { log.log(Level.WARNING, "Could not create dir " + entry.getName()); } } } else { // Create parent dir if necessary File parent = new File(outFile.getParent()); if (!parent.exists() && !parent.mkdirs()) { log.log(Level.WARNING, "Could not create dir " + parent.getAbsolutePath()); } FileOutputStream fos = new FileOutputStream(outFile); ByteStreams.copy(archiveInputStream, fos); fos.close(); } entries++; } if (entries == 0) { throw new IllegalArgumentException("Not able to read any entries from stream (" + archiveInputStream.getBytesRead() + " bytes read from stream)"); } }
Example 12
Source File: CompressedApplicationInputStream.java From vespa with Apache License 2.0 | 5 votes |
private void decompressInto(File application) throws IOException { log.log(Level.FINE, "Application is in " + application.getAbsolutePath()); int entries = 0; ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { log.log(Level.FINE, "Unpacking " + entry.getName()); File outFile = new File(application, entry.getName()); // FIXME/TODO: write more tests that break this logic. I have a feeling it is not very robust. if (entry.isDirectory()) { if (!(outFile.exists() && outFile.isDirectory())) { log.log(Level.FINE, "Creating dir: " + outFile.getAbsolutePath()); boolean res = outFile.mkdirs(); if (!res) { log.log(Level.WARNING, "Could not create dir " + entry.getName()); } } } else { log.log(Level.FINE, "Creating output file: " + outFile.getAbsolutePath()); // Create parent dir if necessary String parent = outFile.getParent(); new File(parent).mkdirs(); FileOutputStream fos = new FileOutputStream(outFile); ByteStreams.copy(ais, fos); fos.close(); } entries++; } if (entries == 0) { log.log(Level.WARNING, "Not able to read any entries from " + application.getName()); } }
Example 13
Source File: CommonsArchiver.java From jarchivelib with Apache License 2.0 | 5 votes |
private void extract(ArchiveInputStream input, File destination) throws IOException { ArchiveEntry entry; while ((entry = input.getNextEntry()) != null) { File file = new File(destination, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { file.getParentFile().mkdirs(); IOUtils.copy(input, file); } FileModeMapper.map(entry, file); } }
Example 14
Source File: FuncotatorReferenceTestUtils.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Extract a fasta tar.gz (should include only one fasta, the index, and a dict). Though this is not enforced. * * Much of this code taken from: http://commons.apache.org/proper/commons-compress/examples.html * * @param fastaTarGz Should include one fasta file, the associated dict file and the associated index, though this is not enforced. Never {@code null} * @param destDir Where to store te untarred files. Never {@code null} * @return a path (as string) to the fasta file. If multiple fasta files were in the tar gz, it will return that last one seen. */ private static String extractFastaTarGzToTemp(final File fastaTarGz, final Path destDir) { Utils.nonNull(fastaTarGz); Utils.nonNull(destDir); String result = null; try (final ArchiveInputStream i = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(fastaTarGz)))) { ArchiveEntry entry = null; while ((entry = i.getNextEntry()) != null) { if (!i.canReadEntryData(entry)) { logger.warn("Could not read an entry in the archive: " + entry.getName() + " from " + fastaTarGz.getAbsolutePath()); } final String name = destDir + "/" + entry.getName(); if (entry.getName().endsWith(".fasta")) { result = name; } final File f = new File(name); if (entry.isDirectory()) { if (!f.isDirectory() && !f.mkdirs()) { throw new IOException("Failed to create directory " + f); } } else { File parent = f.getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) { throw new IOException("Failed to create directory " + parent); } try (OutputStream o = Files.newOutputStream(f.toPath())) { IOUtils.copy(i, o); } } } } catch (final IOException ioe) { throw new GATKException("Could not untar test reference: " + fastaTarGz, ioe); } return result; }
Example 15
Source File: ArchiveExtractor.java From carbon-kernel with Apache License 2.0 | 5 votes |
/** * Extract the file and create directories accordingly. * * @param is pass the stream, because the stream is different for various file types * @param targetDir target directory * @throws IOException */ private static void extract(ArchiveInputStream is, File targetDir) throws IOException { try { if (targetDir.exists()) { FileUtils.forceDelete(targetDir); } createDirectory(targetDir); ArchiveEntry entry = is.getNextEntry(); while (entry != null) { String name = entry.getName(); name = name.substring(name.indexOf("/") + 1); File file = new File(targetDir, name); if (entry.isDirectory()) { createDirectory(file); } else { createDirectory(file.getParentFile()); OutputStream os = new FileOutputStream(file); try { IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } } entry = is.getNextEntry(); } } finally { try { is.close(); } catch (IOException e) { logger.warn("Error occurred while closing the stream", e); } } }
Example 16
Source File: ArchiveUtils.java From deeplearning4j with Apache License 2.0 | 5 votes |
protected static List<String> tarGzListFiles(File file, boolean isTarGz) throws IOException { try(TarArchiveInputStream tin = isTarGz ? new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) : new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(file)))) { ArchiveEntry entry; List<String> out = new ArrayList<>(); while((entry = tin.getNextTarEntry()) != null){ String name = entry.getName(); out.add(name); } return out; } }
Example 17
Source File: InitMojo.java From helm-maven-plugin with MIT License | 4 votes |
protected void downloadAndUnpackHelm() throws MojoExecutionException { Path directory = Paths.get(getHelmExecutableDirectory()); if (Files.exists(directory.resolve(SystemUtils.IS_OS_WINDOWS ? "helm.exe" : "helm"))) { getLog().info("Found helm executable, skip init."); return; } String url = getHelmDownloadUrl(); if (StringUtils.isBlank(url)) { String os = getOperatingSystem(); String architecture = getArchitecture(); String extension = getExtension(); url = String.format("https://get.helm.sh/helm-v%s-%s-%s.%s", getHelmVersion(), os, architecture, extension); } getLog().debug("Downloading Helm: " + url); boolean found = false; try (InputStream dis = new URL(url).openStream(); InputStream cis = createCompressorInputStream(dis); ArchiveInputStream is = createArchiverInputStream(cis)) { // create directory if not present Files.createDirectories(directory); // get helm executable entry ArchiveEntry entry = null; while ((entry = is.getNextEntry()) != null) { String name = entry.getName(); if (entry.isDirectory() || (!name.endsWith("helm.exe") && !name.endsWith("helm"))) { getLog().debug("Skip archive entry with name: " + name); continue; } getLog().debug("Use archive entry with name: " + name); Path helm = directory.resolve(name.endsWith(".exe") ? "helm.exe" : "helm"); try (FileOutputStream output = new FileOutputStream(helm.toFile())) { IOUtils.copy(is, output); } addExecPermission(helm); found = true; break; } } catch (IOException e) { throw new MojoExecutionException("Unable to download and extract helm executable.", e); } if (!found) { throw new MojoExecutionException("Unable to find helm executable in tar file."); } }
Example 18
Source File: UnZip.java From DataHubSystem with GNU Affero General Public License v3.0 | 4 votes |
public static void unCompress (String zip_file, String output_folder) throws IOException, CompressorException, ArchiveException { ArchiveInputStream ais = null; ArchiveStreamFactory asf = new ArchiveStreamFactory (); FileInputStream fis = new FileInputStream (new File (zip_file)); if (zip_file.toLowerCase ().endsWith (".tar")) { ais = asf.createArchiveInputStream ( ArchiveStreamFactory.TAR, fis); } else if (zip_file.toLowerCase ().endsWith (".zip")) { ais = asf.createArchiveInputStream ( ArchiveStreamFactory.ZIP, fis); } else if (zip_file.toLowerCase ().endsWith (".tgz") || zip_file.toLowerCase ().endsWith (".tar.gz")) { CompressorInputStream cis = new CompressorStreamFactory (). createCompressorInputStream (CompressorStreamFactory.GZIP, fis); ais = asf.createArchiveInputStream (new BufferedInputStream (cis)); } else { try { fis.close (); } catch (IOException e) { LOGGER.warn ("Cannot close FileInputStream:", e); } throw new IllegalArgumentException ( "Format not supported: " + zip_file); } File output_file = new File (output_folder); if (!output_file.exists ()) output_file.mkdirs (); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry ()) != null) { File ftemp = new File (output_folder, nextEntry.getName ()); if (nextEntry.isDirectory ()) { ftemp.mkdir (); } else { FileOutputStream fos = FileUtils.openOutputStream (ftemp); IOUtils.copy (ais, fos); fos.close (); } } ais.close (); fis.close (); }
Example 19
Source File: FilesArchiveCompressController.java From MyBox with Apache License 2.0 | 4 votes |
@Override public void donePost() { tableView.refresh(); if (miaoCheck.isSelected()) { FxmlControl.miao3(); } if (openCheck.isSelected()) { File path = targetFile.getParentFile(); browseURI(path.toURI()); recordFileOpened(path); } if (archive == null) { return; } StringBuilder s = new StringBuilder(); s.append("<h1 class=\"center\">").append(targetFile).append("</h1>\n"); s.append("<hr>\n"); int ratio; if (totalSize > 0) { ratio = (int) (100 - targetFile.length() * 100 / totalSize); } else { ratio = 0; } String compressInfo = message("TotalSize") + ":" + FileTools.showFileSize(totalSize) + " " + message("SizeAfterArchivedCompressed") + ":" + FileTools.showFileSize(targetFile.length()) + " " + message("CompressedRatio") + ":" + ratio + "%"; s.append("<P>").append(compressInfo).append("</P>\n"); List<String> names = new ArrayList<>(); names.addAll(Arrays.asList(message("ID"), message("Directory"), message("File"), message("Size"), message("ModifiedTime") )); StringTable table = new StringTable(names, message("ArchiveContents")); int id = 1; String dir, file, size; for (ArchiveEntry entry : archive) { List<String> row = new ArrayList<>(); if (entry.isDirectory()) { dir = entry.getName(); file = ""; } else { int pos = entry.getName().lastIndexOf('/'); if (pos < 0) { dir = ""; file = entry.getName(); } else { dir = entry.getName().substring(0, pos); file = entry.getName().substring(pos + 1, entry.getName().length()); } } if (entry.getSize() > 0) { size = FileTools.showFileSize(entry.getSize()); } else { size = ""; } row.addAll(Arrays.asList((id++) + "", dir, file, size, DateTools.datetimeToString(entry.getLastModifiedDate()) )); table.add(row); } s.append(StringTable.tableDiv(table)); HtmlTools.editHtml(message("ArchiveContents"), s.toString()); }
Example 20
Source File: TarDirectoryStream.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 4 votes |
@Override protected String getName(ArchiveEntry entry) { return entry.getName(); }