List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory
ArchiveStreamFactory
From source file:org.m1theo.apt.repo.AptRepoMojo.java
public void execute() throws MojoExecutionException { getLog().info("repo dir: " + repoDir.getPath()); if (!repoDir.exists()) { repoDir.mkdirs();/*from ww w . j a v a 2 s.c om*/ } try { Collection<Artifact> artifacts = Utils.getAllArtifacts4Type(project, type, aggregate); // Collection<Artifact> artifacts = // Utils.getDebArtifacts(project, reactorProjects, type, aggregate, getLog()); for (Artifact artifact : artifacts) { getLog().debug("Artifact: " + artifact); getLog().debug("Artifact type: " + artifact.getType()); FileUtils.copyFileToDirectory(artifact.getFile(), repoDir); } } catch (IOException e) { getLog().error(FAILED_TO_CREATE_APT_REPO, e); throw new MojoExecutionException(FAILED_TO_CREATE_APT_REPO, e); } File[] files = repoDir.listFiles(new FileFilter() { private String ext = "." + type; public boolean accept(File pathname) { if (pathname.getName().endsWith(ext)) { return true; } return false; } }); Packages packages = new Packages(); for (int i = 0; i < files.length; i++) { File file = files[i]; PackageEntry packageEntry = new PackageEntry(); packageEntry.setSize(file.length()); packageEntry.setSha1(Utils.getDigest("SHA-1", file)); packageEntry.setSha256(Utils.getDigest("SHA-256", file)); packageEntry.setMd5sum(Utils.getDigest("MD5", file)); // String fileName = debFilesDir.getName() + File.separator + file.getName(); String fileName = file.getName(); packageEntry.setFilename(fileName); getLog().info("found deb: " + fileName); try { ArchiveInputStream control_tgz; ArArchiveEntry entry; TarArchiveEntry control_entry; ArchiveInputStream debStream = new ArchiveStreamFactory().createArchiveInputStream("ar", new FileInputStream(file)); while ((entry = (ArArchiveEntry) debStream.getNextEntry()) != null) { if (entry.getName().equals("control.tar.gz")) { ControlHandler controlHandler = new ControlHandler(); GZIPInputStream gzipInputStream = new GZIPInputStream(debStream); control_tgz = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream); while ((control_entry = (TarArchiveEntry) control_tgz.getNextEntry()) != null) { getLog().debug("control entry: " + control_entry.getName()); if (control_entry.getName().equals(CONTROL_FILE_NAME)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(control_tgz, outputStream); String content_string = outputStream.toString("UTF-8"); outputStream.close(); controlHandler.setControlContent(content_string); getLog().debug("control cont: " + outputStream.toString("utf-8")); break; } } control_tgz.close(); if (controlHandler.hasControlContent()) { controlHandler.handle(packageEntry); } else { throw new MojoExecutionException("no control content found for: " + file.getName()); } break; } } debStream.close(); packages.addPackageEntry(packageEntry); if (attach) { getLog().info("Attaching file: " + file); projectHelper.attachArtifact(project, type, file.getName(), file); // projectHelper.attachArtifact(project, file, fileName); } } catch (MojoExecutionException e) { String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName(); getLog().error(msg, e); throw new MojoExecutionException(msg, e); } catch (FileNotFoundException e) { String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName(); getLog().error(msg, e); throw new MojoExecutionException(msg, e); } catch (ArchiveException e) { String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName(); getLog().error(msg, e); throw new MojoExecutionException(msg, e); } catch (IOException e) { String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName(); getLog().error(msg, e); throw new MojoExecutionException(msg, e); } } try { File packagesFile = new File(repoDir, PACKAGES_GZ); packagesWriter = new BufferedWriter( new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(packagesFile)))); packagesWriter.write(packages.toString()); // FileUtils.fileWrite(packagesFile, packages.toString()); DefaultHashes hashes = Utils.getDefaultDigests(packagesFile); ReleaseInfo pinfo = new ReleaseInfo(PACKAGES_GZ, packagesFile.length(), hashes); Release release = new Release(); release.addInfo(pinfo); final File releaseFile = new File(repoDir, RELEASE); FileUtils.fileWrite(releaseFile, release.toString()); // if (attach) { // getLog().info("Attaching created apt-repo files: " + releaseFile + ", " + packagesFile); // projectHelper.attachArtifact(project, "gz", "Packages", packagesFile); // projectHelper.attachArtifact(project, "Release-File", "Release", packagesFile); // } } catch (IOException e) { throw new MojoExecutionException("writing files failed", e); } finally { if (packagesWriter != null) { try { packagesWriter.close(); } catch (IOException e) { throw new MojoExecutionException("writing files failed", e); } } } }
From source file:org.m1theo.apt.repo.builder.RepoBuilder.java
private void createPackagesFile() throws AptRepoException { //TODO implement copy file? Packages packages = new Packages(); for (File file : debFiles) { PackageEntry packageEntry = new PackageEntry(); packageEntry.setSize(file.length()); packageEntry.setSha1(Utils.getDigest("SHA-1", file)); packageEntry.setSha256(Utils.getDigest("SHA-256", file)); packageEntry.setSha512(Utils.getDigest("SHA-512", file)); packageEntry.setMd5sum(Utils.getDigest("MD5", file)); String fileName = file.getName(); packageEntry.setFilename(fileName); //getLog().info("found deb: " + fileName); try {/*ww w . j a v a 2 s .c o m*/ ArchiveInputStream control_tgz; ArArchiveEntry entry; TarArchiveEntry control_entry; ArchiveInputStream debStream = new ArchiveStreamFactory().createArchiveInputStream("ar", new FileInputStream(file)); while ((entry = (ArArchiveEntry) debStream.getNextEntry()) != null) { if (entry.getName().equals("control.tar.gz")) { ControlHandler controlHandler = new ControlHandler(); GZIPInputStream gzipInputStream = new GZIPInputStream(debStream); control_tgz = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream); while ((control_entry = (TarArchiveEntry) control_tgz.getNextEntry()) != null) { //getLog().debug("control entry: " + control_entry.getName()); if (control_entry.getName().equals(CONTROL_FILE_NAME)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(control_tgz, outputStream); String content_string = outputStream.toString("UTF-8"); outputStream.close(); controlHandler.setControlContent(content_string); //getLog().debug("control cont: " + outputStream.toString("utf-8")); break; } } control_tgz.close(); if (controlHandler.hasControlContent()) { controlHandler.handle(packageEntry); } else { throw new AptRepoException("no control content found for: " + file.getName()); } break; } } debStream.close(); packages.addPackageEntry(packageEntry); } catch (UnsupportedEncodingException e) { throw new AptRepoException("Packages encoding exception", e); } catch (ArchiveException e) { throw new AptRepoException("Packages archive exception", e); } catch (IOException e) { throw new AptRepoException("Packages IOExeption", e); } } try { File packagesFile = new File(repoDir, PACKAGES); BufferedWriter packagesWriter = null; packagesWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(packagesFile))); packagesWriter.write(packages.toString()); packagesWriter.close(); this.packagesFile = packagesFile; File packagesGzFile = new File(repoDir, PACKAGES_GZ); BufferedWriter packagesGzWriter = new BufferedWriter( new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(packagesGzFile)))); packagesGzWriter.write(packages.toString()); packagesGzWriter.close(); this.packagesGzFile = packagesGzFile; } catch (FileNotFoundException e) { throw new AptRepoException("invalid repodir: " + repoDir, e); } catch (IOException e) { throw new AptRepoException("writing packages failed", e); } }
From source file:org.mitre.xtext.converters.ArchiveNavigator.java
public File unzip(File zipFile) throws IOException { String _working = tempDir.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(zipFile.getPath()); File workingDir = new File(_working); workingDir.mkdir();// www . j a v a 2 s. c om InputStream input = new BufferedInputStream(new FileInputStream(zipFile)); try { ZipArchiveInputStream in = (ZipArchiveInputStream) (new ArchiveStreamFactory() .createArchiveInputStream("zip", input)); ZipArchiveEntry zipEntry; while ((zipEntry = (ZipArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(zipEntry)) { continue; } try { File tmpFile = saveArchiveEntry(zipEntry, in, _working); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + zipEntry.getName() + "!" + zipEntry.getName(), err); } } in.close(); } catch (ArchiveException ae) { throw new IOException(ae); } return workingDir; }
From source file:org.mitre.xtext.converters.ArchiveNavigator.java
public File untar(File tarFile) throws IOException { String _working = tempDir.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(tarFile.getPath()); File workingDir = new File(_working); workingDir.mkdir();//ww w. j a v a2s . c o m InputStream input = new BufferedInputStream(new FileInputStream(tarFile)); try { TarArchiveInputStream in = (TarArchiveInputStream) (new ArchiveStreamFactory() .createArchiveInputStream("tar", input)); TarArchiveEntry tarEntry; while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(tarEntry)) { continue; } try { File tmpFile = saveArchiveEntry(tarEntry, in, _working); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err); } } in.close(); } catch (ArchiveException ae) { throw new IOException(ae); } return workingDir; }
From source file:org.moe.cli.manager.PrebuildCocoaPodsManager.java
public IExecutor processCocoapods(String source, SpecObject spec, String packageName, String[] javaSource, String outputJar) throws IOException, CompressorException, ArchiveException, InvalidParameterSpecException, InterruptedException, URISyntaxException, UnsupportedTypeException { File tmpFolder = NatJFileUtils.getNewTempDirectory(); File download = new File(tmpFolder, "download/"); if (!download.exists()) { download.mkdirs();// w w w. ja va2 s. c om } int nameIdx = source.lastIndexOf("/"); File outFile = new File(download, source.substring(nameIdx + 1)); GrabUtils.download(new URI(source), outFile); int extIdx = outFile.getName().lastIndexOf("."); String folderUnzip = outFile.getName().substring(0, extIdx); File destination = new File(download, folderUnzip); if (!destination.exists()) { destination.mkdirs(); } String type = spec.getSource().get("type"); if (outFile.getName().endsWith(".zip") || (type != null && type.equals("zip"))) { ZipFile apachZip = new ZipFile(outFile); ArchiveUtils.unzipArchive(apachZip, destination); } else if (outFile.getName().endsWith("tar.bzip2") || outFile.getName().endsWith("tar.gz") || outFile.getName().endsWith("tar.bz2") || type.equals("tgz")) { InputStream inputC = null; int extArchIdx = outFile.getName().lastIndexOf("."); String ext = outFile.getName().substring(extArchIdx + 1); if (ext.equals("tar")) { inputC = new FileInputStream(outFile); } else if (ext.equals("bzip2") || ext.equals("gz") || ext.equals("bz2")) { InputStream fin = new FileInputStream(outFile); String compressorType = null; if (ext.equals("bzip2") || ext.equals("bz2")) { compressorType = CompressorStreamFactory.BZIP2; } else if (ext.equals("gz") || type.equals("tgz")) { compressorType = CompressorStreamFactory.GZIP; } inputC = new CompressorStreamFactory().createCompressorInputStream(compressorType, fin); } else { throw new InvalidParameterException("Unsupported archive type"); } ArchiveInputStream input = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, inputC); ArchiveUtils.untarArchive(input, destination); } //update destination for git repo if (spec.getSource().containsKey("git")) { String git = spec.getSource().get("git"); String tag = spec.getSource().get("tag"); int gitNameIdx = git.lastIndexOf("/"); String gitName = git.substring(gitNameIdx + 1); if (gitName.endsWith(".git")) { gitName = gitName.substring(0, gitName.length() - 4); } if (!Character.isDigit(tag.charAt(0)) && Character.isDigit(tag.charAt(1))) { tag = tag.substring(1); } String zipSubfolder = String.format("%s-%s", gitName, tag); destination = new File(destination, zipSubfolder); } List<String> commandList = spec.getPreparedCommands(); for (String command : commandList) { if (command != null && !command.isEmpty()) { executePrepareCommands(destination, command); } } IExecutor executor = null; //find all bundles Set<String> bundleContent = new HashSet<String>(); List<String> resources = spec.getResources(); if (resources != null && resources.size() > 0) { for (String bundle : resources) { Set<String> bundleWildCard = getBundleResources(bundle, destination); bundleContent.addAll(bundleWildCard); } } String[] bundleRes = bundleContent.toArray(new String[0]); //get additional linker flags String ldFlags = spec.getLdFlags(); //create Set<String> headersContent = new HashSet<String>(); List<String> headerList = spec.getSourceFiles(); if (headerList != null && headerList.size() > 0) { for (String header : headerList) { int recursivIdx = header.indexOf("**"); if (recursivIdx >= 0) { File headerFile = new File(destination, header.substring(0, recursivIdx)); headersContent.add(headerFile.getPath()); } else { Set<String> bundleWildCard = getBundleResources(header, destination); headersContent.addAll(bundleWildCard); } } } if (spec.getVendoredFrameworks() != null && spec.getVendoredFrameworks().size() > 0) { List<File> frameworkList = new ArrayList<>(); Set<String> frameworkContent = new HashSet<String>(); for (String vFramework : spec.getVendoredFrameworks()) { Set<String> frName = getBundleResources(vFramework, destination); if (frName.size() != 1) throw new RuntimeException( "Something wrong with this code. Refactor it! And the same with libraries"); File frameworkFile = new File(frName.toArray(new String[0])[0]); if (frameworkFile.exists()) { frameworkList.add(frameworkFile); frameworkContent.add(frameworkFile.getPath()); int frameworkNameIdx = frameworkFile.getName().lastIndexOf("."); if (frameworkNameIdx >= 0) { ldFlags = ldFlags + "-framework " + frameworkFile.getName().substring(0, frameworkNameIdx) + ";"; } } } if (headersContent.isEmpty()) { for (File framework : frameworkList) { File headerFile = new File(framework, "Headers"); headersContent.add(headerFile.getPath()); } } executor = new ThirdPartyFrameworkLinkExecutor(packageName, frameworkContent.toArray(new String[0]), javaSource, headersContent.toArray(new String[0]), bundleRes, outputJar, ldFlags); } else if (spec.getVendoredLibraries() != null && spec.getVendoredLibraries().size() > 0) { Set<String> libContent = new HashSet<String>(); for (String lib : spec.getVendoredLibraries()) { Set<String> libName = getBundleResources(lib, destination); if (libName.size() != 1) throw new RuntimeException( "Something wrong with this code. Refactor it! And the same with libraries"); File library = new File(libName.toArray(new String[0])[0]); if (library.exists()) { libContent.add(library.getPath()); int libNameIdx = library.getName().lastIndexOf("."); if (libNameIdx >= 0) { String libShortName = library.getName().substring(0, libNameIdx); ldFlags = ldFlags + "-l" + (libShortName.startsWith("lib") ? libShortName.substring(3) : libShortName) + ";"; } } } executor = new ThirdPartyLibraryLinkExecutor(packageName, libContent.toArray(new String[0]), javaSource, headersContent.toArray(new String[0]), bundleRes, outputJar, ldFlags); } return executor; }
From source file:org.ng200.openolympus.controller.task.TaskFilesystemManipulatingController.java
@PreAuthorize(SecurityExpressionConstants.IS_ADMIN) private void extractZipFile(final InputStream zipFile, final Path destination) throws Exception { try (ArchiveInputStream input = new ArchiveStreamFactory() .createArchiveInputStream(new BufferedInputStream(zipFile))) { ArchiveEntry entry;//from w w w. j a v a 2 s .c o m while ((entry = input.getNextEntry()) != null) { final Path dest = destination.resolve(entry.getName()); if (entry.isDirectory()) { FileAccess.createDirectories(dest); } else { FileAccess.createDirectories(dest.getParent()); FileAccess.createFile(dest); Files.copy(input, dest, StandardCopyOption.REPLACE_EXISTING); } } } }
From source file:org.ng200.openolympus.controller.task.TaskUploader.java
protected void uploadTaskData(final Task task, final UploadableTask taskDto) throws IOException, IllegalStateException, FileNotFoundException, ArchiveException { final String descriptionPath = MessageFormat.format(TaskUploader.TASK_DESCRIPTION_PATH_TEMPLATE, StorageSpace.STORAGE_PREFIX, task.getTaskLocation()); if (taskDto.getDescriptionFile() != null) { final File descriptionFile = new File(descriptionPath); if (descriptionFile.exists()) { descriptionFile.delete();//www . jav a 2 s . c om } descriptionFile.getParentFile().mkdirs(); descriptionFile.createNewFile(); taskDto.getDescriptionFile().transferTo(descriptionFile); } if (taskDto.getTaskZip() != null) { final String checkingChainPath = MessageFormat.format(TaskUploader.TASK_CHECKING_CHAIN_PATH_TEMPLATE, StorageSpace.STORAGE_PREFIX, task.getTaskLocation()); final File checkingChainFile = new File(checkingChainPath); if (checkingChainFile.exists()) { FileUtils.deleteDirectory(checkingChainFile); } checkingChainFile.mkdirs(); try (ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", taskDto.getTaskZip().getInputStream())) { ZipArchiveEntry entry; while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) { final File file = new File(checkingChainFile, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { try (OutputStream out = new FileOutputStream(file)) { IOUtils.copy(in, out); } } } } } }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Untar an input file into an output file. * //from w w w. j a v a 2s . c o m * The output file is created in the output folder, having the same name as the input file, * minus the '.tar' extension. * * @param inFile * the input .tar file * @param outputDir * the output directory file. * @throws IOException * @throws FileNotFoundException * * @return The {@link List} of {@link File}s with the untared content. * @throws ArchiveException */ public static List<File> untar(final File inFile, final File outputDir) { final List<File> untaredFiles = new LinkedList<File>(); try { final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { File parentFile = outputFile.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) { outputFile.setExecutable(true, true); } outputFile.setReadable(true); outputFile.setWritable(true, true); } untaredFiles.add(outputFile); } debInputStream.close(); } catch (Exception e) { LOGGER.error("Error while untar {} file by {}", inFile, e.getMessage()); LOGGER.debug("Trace is : ", e); throw new NGrinderRuntimeException("Error while untar file", e); } return untaredFiles; }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Untar an input file into an output file. * The output file is created in the output folder, having the same name as * the input file, minus the '.tar' extension. * * @param inFile the input .tar file/*from ww w. j av a 2 s.co m*/ * @param outputDir the output directory file. * @return The {@link List} of {@link File}s with the untared content. */ @SuppressWarnings("resource") public static List<File> untar(final File inFile, final File outputDir) { final List<File> untaredFiles = new LinkedList<File>(); InputStream is = null; TarArchiveInputStream debInputStream = null; try { is = new FileInputStream(inFile); debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { File parentFile = outputFile.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } final OutputStream outputFileStream = new FileOutputStream(outputFile); try { IOUtils.copy(debInputStream, outputFileStream); } finally { IOUtils.closeQuietly(outputFileStream); } if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) { outputFile.setExecutable(true, true); } outputFile.setReadable(true); outputFile.setWritable(true, true); } untaredFiles.add(outputFile); } } catch (Exception e) { throw processException("Error while untar file", e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(debInputStream); } return untaredFiles; }
From source file:org.obm.push.arquillian.ManagedTomcatInstaller.java
private static void uncompressTarFile(File parent, File inputFile) throws FileNotFoundException, IOException, ArchiveException { FileInputStream inputStream = new FileInputStream(inputFile); ArchiveInputStream archiveInputStream = null; try {//from w ww . ja va2s . co m archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, inputStream); ArchiveEntry entry = null; while ((entry = archiveInputStream.getNextEntry()) != null) { File outputFile = new File(parent, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdir()) { throw new IOException( String.format("Unable to create directory %s", outputFile.getAbsolutePath())); } } } else { FileOutputStream outputFileStream = new FileOutputStream(outputFile); try { IOUtils.copy(archiveInputStream, outputFileStream); } finally { outputFileStream.close(); } } } } finally { try { if (archiveInputStream != null) { archiveInputStream.close(); } } finally { inputStream.close(); } } }