List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveInputStream
public ArchiveInputStream createArchiveInputStream(final String archiverName, final InputStream in) throws ArchiveException
From source file:fr.gael.dhus.util.UnZip.java
public static void unCompress(String zip_file, String output_folder) throws IOException, CompressorException, ArchiveException { ArchiveInputStream ais = null;/*from w w w . j a va 2s . c om*/ 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(); }
From source file:io.github.retz.executor.FileManager.java
private static ArchiveInputStream createAIS(File file) throws FileNotFoundException, IOException, ArchiveException { ArchiveStreamFactory factory = new ArchiveStreamFactory(); InputStream in = new BufferedInputStream(new FileInputStream(file)); if (file.getName().endsWith(".tar.gz") || file.getName().endsWith(".tgz")) { return factory.createArchiveInputStream(ArchiveStreamFactory.TAR, new GZIPInputStream(in)); } else if (file.getName().endsWith(".tar.bz2") || file.getName().endsWith(".tar.xz")) { // TODO: "tar, tbz2, txz. See mesos/src/launcher/fetcher.cpp for supported formats LOG.error("TODO: compression on {} must be supported", file.getName()); throw new RuntimeException(); }//from w ww . ja v a2 s . c o m LOG.error("Not decompressing. File with unsupported suffix: {}", file); return null; }
From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java
/** * Unpacks a file//from ww w . ja va2 s . c om * @param source source file * @param destination destination directory. If the directory does not * exists, it will be created * @param format archive format * @return the number of bytes processed * @throws SspsException * @throws ArchiveException * @throws IOException */ public static long unpack(File source, File destination, String format) throws SspsException, ArchiveException, IOException { if (!destination.exists()) { if (!destination.mkdirs()) { throw new IOException("Unable to create destination directory: " + destination.getPath()); } } else { if (!destination.isDirectory()) { throw new SspsException( "The provided destination " + destination.getPath() + " is not a directory"); } } ArchiveStreamFactory factory = new ArchiveStreamFactory(); FileInputStream inputFileStream = new FileInputStream(source); ArchiveInputStream archiveStream; try { archiveStream = factory.createArchiveInputStream(format, inputFileStream); } catch (ArchiveException e) { IOUtils.closeQuietly(inputFileStream); throw e; } OutputStream outStream = null; try { TarArchiveEntry entry = (TarArchiveEntry) archiveStream.getNextEntry(); while (entry != null) { File outFile = new File(destination, entry.getName()); if (entry.isDirectory()) { if (!outFile.exists()) { if (!outFile.mkdirs()) { throw new SspsException("Unable to create directory: " + outFile.getPath()); } } else { logger.warn("Directory " + outFile.getPath() + " already exists. Ignoring ..."); } } else { File parent = outFile.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create parent directories " + parent.getPath()); } } outStream = new FileOutputStream(outFile); IOUtils.copy(archiveStream, outStream); outStream.close(); } int mode = entry.getMode(); PermissionsUtils.setPermissions(mode, outFile); entry = (TarArchiveEntry) archiveStream.getNextEntry(); } inputFileStream.close(); archiveStream.close(); } finally { IOUtils.closeQuietly(outStream); IOUtils.closeQuietly(inputFileStream); IOUtils.closeQuietly(archiveStream); } return archiveStream.getBytesRead(); }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
private static Boolean archiveContainsFile(File archive, String archiveType, String fileToLookFor) { ArchiveInputStream ais = null;//from w ww . j a v a 2 s . c om try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive)); ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { if (fileToLookFor.equals(entry.getName())) { return Boolean.TRUE; } } return Boolean.FALSE; } catch (ArchiveException e) { throw new RuntimeException(e); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(ais); } }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
private static Map<String, File> extractFiles(File archive, String archiveType, String searchFor, Boolean isRegExp) {/* w w w.j av a 2 s.c o m*/ Map<String, File> result = new HashMap<String, File>(); ArchiveInputStream ais = null; try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive)); ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { if (isRegExp) { if (!matches(entry.getName(), searchFor)) { continue; } } else if (!searchFor.equals(entry.getName())) { continue; } File physicalFile = FileUtils.createEmptyFileWithSamePermissions(archive, archive.getName() + "!" + Paths.get(entry.getName()).getFileName().toString() + "_"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(physicalFile)); byte[] content = new byte[2048]; int len; while ((len = ais.read(content)) != -1) { bos.write(content, 0, len); } bos.close(); content = null; result.put(entry.getName(), physicalFile); } } catch (ArchiveException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(ais); } return result; }
From source file:fr.gael.ccsds.sip.archive.TarArchiveManager.java
/** * Produces TAR archive//from w w w . jav a 2 s . com */ @Override public File copy(final File src, final File tar_file, final String dst) throws Exception { final ArchiveStreamFactory asf = new ArchiveStreamFactory(); // Case of tar already exist: all the entries must be copied.. if (tar_file.exists()) { final FileInputStream fis = new FileInputStream(tar_file); final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis); final File tempFile = File.createTempFile("updateTar", "tar"); final FileOutputStream fos = new FileOutputStream(tempFile); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); ais.close(); aos.close(); fis.close(); // copies the new file over the old tar_file.delete(); tempFile.renameTo(tar_file); return tar_file; } else { final FileOutputStream fos = new FileOutputStream(tar_file); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos); // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); aos.close(); fos.close(); } return tar_file; }
From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java
@Override public File copy(final File src, final File tar_file, final String dst) throws Exception { final ArchiveStreamFactory asf = new ArchiveStreamFactory(); final CompressorStreamFactory csf = new CompressorStreamFactory(); // Case of tar already exist: all the entries must be copied.. if (tar_file.exists()) { final FileInputStream fis = new FileInputStream(tar_file); final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis); final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis); final File tempFile = File.createTempFile("updateTar", "tar"); final FileOutputStream fos = new FileOutputStream(tempFile); final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos); // copy the existing entries ArchiveEntry nextEntry;//w ww .ja va 2 s .c om while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); ais.close(); aos.close(); fis.close(); // copies the new file over the old tar_file.delete(); tempFile.renameTo(tar_file); return tar_file; } else { final FileOutputStream fos = new FileOutputStream(tar_file); final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos); // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); aos.close(); fos.close(); } return tar_file; }
From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java
/** * {@inheritDoc}//from ww w .j ava 2 s.c om */ @Override public void storeChunk(InputStream inStream) throws ArchiveException, IOException { boolean lastChunk = false; String chunkName = "unknown-" + System.currentTimeMillis(); ArchiveStreamFactory f = new ArchiveStreamFactory(); ArchiveInputStream ais = f.createArchiveInputStream("tar", inStream); VirtualVehicleHolder virtualVehicleHolder = new VirtualVehicleHolder(); for (TarArchiveEntry entry = (TarArchiveEntry) ais .getNextEntry(); entry != null; entry = (TarArchiveEntry) ais.getNextEntry()) { chunkName = entry.getName(); if (chunkName.startsWith("vv/")) { lastChunk |= storeVirtualVehicleEntry(ais, entry, virtualVehicleHolder); logMigratedChunk(chunkName, virtualVehicleHolder.getVirtualVehicle(), lastChunk); } else if (chunkName.startsWith("storage/")) { storeStorageEntry(ais, entry, virtualVehicleHolder.getVirtualVehicle()); logMigratedChunk(chunkName, virtualVehicleHolder.getVirtualVehicle(), lastChunk); } // TODO message queue else { throw new IOException("Can not store unknown type of entry " + chunkName); } } sessionManager.commit(); VirtualVehicle vv = virtualVehicleHolder.getVirtualVehicle(); String result = new JSONObject("uuid", vv.getUuid(), "chunk", vv.getChunkNumber()).toCompactString(); CommunicationResponse response = com.transfer(vv.getMigrationSource(), VvRteConstants.MIGRATION_ACK_CONNECTOR, org.apache.commons.codec.binary.StringUtils.getBytesUtf8(result)); if (response.getStatus() == Status.OK) { if (lastChunk) { VirtualVehicleState newState = VirtualVehicleState.VV_NO_CHANGE_AFTER_MIGRATION .contains(vv.getPreMigrationState()) ? vv.getPreMigrationState() : VirtualVehicleState.MIGRATION_COMPLETED_RCV; vv.setPreMigrationState(null); updateStateAndCommit(vv, newState, null); launcher.stateChange(vv.getId(), vv.getState()); } } else { String content = new String(response.getContent(), "UTF-8"); logger.error("Migration ACK failed! Virtual vehicle: " + vv.getName() + " (" + vv.getUuid() + ") " + content); updateStateAndCommit(vv, VirtualVehicleState.MIGRATION_INTERRUPTED_RCV, content); } }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
public static void replaceFilesInArchive(File archive, String archiveType, Map<String, File> filesToReplace) { ArchiveStreamFactory factory = new ArchiveStreamFactory(); File manifest = null;/* ww w .j ava 2 s.c om*/ InputStream archiveStream = null; ArchiveInputStream ais = null; ArchiveOutputStream aos = null; List<FileInputStream> streamsToClose = new ArrayList<FileInputStream>(); File tmpArchive = FileUtils.createEmptyFileWithSamePermissions(archive); try { aos = factory.createArchiveOutputStream(archiveType, new FileOutputStream(tmpArchive)); ChangeSet changes = new ChangeSet(); if (ArchiveStreamFactory.JAR.equalsIgnoreCase(archiveType)) { manifest = manifestWorkaround(archive, archiveType, aos, changes, streamsToClose); } for (String filePath : filesToReplace.keySet()) { File replaceWithFile = filesToReplace.get(filePath); ArchiveEntry archiveEntry = aos.createArchiveEntry(replaceWithFile, filePath); FileInputStream fis = new FileInputStream(replaceWithFile); streamsToClose.add(fis); changes.add(archiveEntry, fis, true); } archiveStream = new FileInputStream(archive); ais = factory.createArchiveInputStream(archiveType, archiveStream); ChangeSetPerformer performer = new ChangeSetPerformer(changes); performer.perform(ais, aos); } catch (IOException e) { throw new RuntimeException(e); } catch (ArchiveException e) { throw new RuntimeException(e); } finally { for (FileInputStream fis : streamsToClose) { IOUtils.closeQuietly(fis); } IOUtils.closeQuietly(aos); IOUtils.closeQuietly(ais); IOUtils.closeQuietly(archiveStream); } if (manifest != null) { manifest.delete(); } if (!archive.delete()) { throw new RuntimeException("Couldn't delete file [" + archive.getPath() + "]... Aborting!"); } if (!tmpArchive.renameTo(archive)) { throw new RuntimeException("Couldn't rename filtered file from [" + tmpArchive.getPath() + "] to [" + archive.getPath() + "]... Aborting!"); } }
From source file:consulo.cold.runner.execute.target.artifacts.Generator.java
public void buildDistributionInArchive(String distZip, @Nullable String jdkArchivePath, String path, String archiveOutType) throws Exception { myListener.info("Build: " + path); ArchiveStreamFactory factory = new ArchiveStreamFactory(); final File fileZip = new File(myDistPath, distZip); final List<String> executables = Arrays.asList(ourExecutable); try (OutputStream pathStream = createOutputStream(archiveOutType, path)) { ArchiveOutputStream archiveOutputStream = factory.createArchiveOutputStream(archiveOutType, pathStream); if (archiveOutputStream instanceof TarArchiveOutputStream) { ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); }/*from w ww . jav a 2 s. c om*/ // move Consulo to archive, and change permissions try (InputStream is = new FileInputStream(fileZip)) { try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) { ArchiveEntry tempEntry = ais.getNextEntry(); while (tempEntry != null) { final ArchiveEntryWrapper newEntry = createEntry(archiveOutType, tempEntry.getName(), tempEntry); newEntry.setMode(extractMode(tempEntry)); newEntry.setTime(tempEntry.getLastModifiedDate().getTime()); if (executables.contains(tempEntry.getName())) { newEntry.setMode(0b111_101_101); } copyEntry(archiveOutputStream, ais, tempEntry, newEntry); tempEntry = ais.getNextEntry(); } } } boolean mac = distZip.contains("mac"); // jdk check if (jdkArchivePath != null) { try (InputStream is = new FileInputStream(jdkArchivePath)) { try (GzipCompressorInputStream gz = new GzipCompressorInputStream(is)) { try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.TAR, gz)) { ArchiveEntry tempEntry = ais.getNextEntry(); while (tempEntry != null) { final String name = tempEntry.getName(); // is our path if (!mac && name.startsWith("jre/")) { final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType, "Consulo/platform/buildSNAPSHOT/" + name, tempEntry); jdkEntry.setMode(extractMode(tempEntry)); jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime()); copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry); } else if (mac && name.startsWith("jdk")) { boolean needAddToArchive = true; for (String prefix : ourMacSkipJdkList) { if (name.startsWith(prefix)) { needAddToArchive = false; } } if (needAddToArchive) { final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType, "Consulo.app/Contents/platform/buildSNAPSHOT/jre/" + name, tempEntry); jdkEntry.setMode(extractMode(tempEntry)); jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime()); copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry); } } tempEntry = ais.getNextEntry(); } } } } } archiveOutputStream.finish(); } }