List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java
private void sendAnnotationData(List<AnnotationDTO> annoResults, TarArchiveOutputStream tos) throws IOException { InputStream annoIn = null;//from w ww . j a v a 2s . c o m for (AnnotationDTO a : annoResults) { String filePath = a.getFilePath(); String fileName = a.getFileName(); try { File annotationFile = new File(filePath); ArchiveEntry tarArchiveEntry = tos.createArchiveEntry(annotationFile, fileName); annoIn = new FileInputStream(annotationFile); tos.putArchiveEntry(tarArchiveEntry); IOUtils.copy(annoIn, tos); tos.closeArchiveEntry(); } catch (FileNotFoundException e) { e.printStackTrace(); // just print the exception and continue the loop so rest of // images will get download. } finally { IOUtils.closeQuietly(annoIn); logger.info("DownloadServlet Annotation transferred at " + new Date().getTime()); } } }
From source file:com.gitblit.servlet.PtServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {/* w ww . j a va2s. co m*/ response.setContentType("application/octet-stream"); response.setDateHeader("Last-Modified", lastModified); response.setHeader("Cache-Control", "none"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); boolean windows = false; try { String useragent = request.getHeader("user-agent").toString(); windows = useragent.toLowerCase().contains("windows"); } catch (Exception e) { } byte[] pyBytes; File file = runtimeManager.getFileOrFolder("tickets.pt", "${baseFolder}/pt.py"); if (file.exists()) { // custom script pyBytes = readAll(new FileInputStream(file)); } else { // default script pyBytes = readAll(getClass().getResourceAsStream("/pt.py")); } if (windows) { // windows: download zip file with pt.py and pt.cmd response.setHeader("Content-Disposition", "attachment; filename=\"pt.zip\""); OutputStream os = response.getOutputStream(); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); // add the Python script ZipArchiveEntry pyEntry = new ZipArchiveEntry("pt.py"); pyEntry.setSize(pyBytes.length); pyEntry.setUnixMode(FileMode.EXECUTABLE_FILE.getBits()); pyEntry.setTime(lastModified); zos.putArchiveEntry(pyEntry); zos.write(pyBytes); zos.closeArchiveEntry(); // add a Python launch cmd file byte[] cmdBytes = readAll(getClass().getResourceAsStream("/pt.cmd")); ZipArchiveEntry cmdEntry = new ZipArchiveEntry("pt.cmd"); cmdEntry.setSize(cmdBytes.length); cmdEntry.setUnixMode(FileMode.REGULAR_FILE.getBits()); cmdEntry.setTime(lastModified); zos.putArchiveEntry(cmdEntry); zos.write(cmdBytes); zos.closeArchiveEntry(); // add a brief readme byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt")); ZipArchiveEntry txtEntry = new ZipArchiveEntry("readme.txt"); txtEntry.setSize(txtBytes.length); txtEntry.setUnixMode(FileMode.REGULAR_FILE.getBits()); txtEntry.setTime(lastModified); zos.putArchiveEntry(txtEntry); zos.write(txtBytes); zos.closeArchiveEntry(); // cleanup zos.finish(); zos.close(); os.flush(); } else { // unix: download a tar.gz file with pt.py set with execute permissions response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\""); OutputStream os = response.getOutputStream(); CompressorOutputStream cos = new CompressorStreamFactory() .createCompressorOutputStream(CompressorStreamFactory.GZIP, os); TarArchiveOutputStream tos = new TarArchiveOutputStream(cos); tos.setAddPaxHeadersForNonAsciiNames(true); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); // add the Python script TarArchiveEntry pyEntry = new TarArchiveEntry("pt"); pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits()); pyEntry.setModTime(lastModified); pyEntry.setSize(pyBytes.length); tos.putArchiveEntry(pyEntry); tos.write(pyBytes); tos.closeArchiveEntry(); // add a brief readme byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt")); TarArchiveEntry txtEntry = new TarArchiveEntry("README"); txtEntry.setMode(FileMode.REGULAR_FILE.getBits()); txtEntry.setModTime(lastModified); txtEntry.setSize(txtBytes.length); tos.putArchiveEntry(txtEntry); tos.write(txtBytes); tos.closeArchiveEntry(); // cleanup tos.finish(); tos.close(); cos.close(); os.flush(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.dotcms.publisher.myTest.PushPublisher.java
/** * Does the work of compression and going recursive for nested directories * <p/>//w w w. j ava 2 s .com * * * @param taos The archive * @param file The file to add to the archive * @param dir The directory that should serve as the parent directory in the archivew * @throws IOException */ private void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir, String bundleRoot) throws IOException { if (!file.isHidden()) { // Create an entry for the file if (!dir.equals(".")) taos.putArchiveEntry(new TarArchiveEntry(file, dir + File.separator + file.getName())); if (file.isFile()) { // Add the file to the archive BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(new FileInputStream(file), taos); taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { //Logger.info(this.getClass(),file.getPath().substring(bundleRoot.length())); // close the archive entry if (!dir.equals(".")) taos.closeArchiveEntry(); // go through all the files in the directory and using recursion, add them to the archive for (File childFile : file.listFiles()) { addFilesToCompression(taos, childFile, file.getPath().substring(bundleRoot.length()), bundleRoot); } } } }
From source file:com.st.maven.debian.DebianPackageMojo.java
private void fillControlTar(Config config, ArFileOutputStream output) throws MojoExecutionException { TarArchiveOutputStream tar = null; try {/* w ww . j av a 2 s . co m*/ tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output))); tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry rootDir = new TarArchiveEntry("./"); tar.putArchiveEntry(rootDir); tar.closeArchiveEntry(); byte[] controlData = processTemplate(freemarkerConfig, config, "control.ftl"); TarArchiveEntry controlEntry = new TarArchiveEntry("./control"); controlEntry.setSize(controlData.length); tar.putArchiveEntry(controlEntry); tar.write(controlData); tar.closeArchiveEntry(); byte[] preinstBaseData = processTemplate("preinst", freemarkerConfig, config, combine("preinst.ftl", BASE_DIR + File.separator + "preinst", false)); long size = preinstBaseData.length; TarArchiveEntry preinstEntry = new TarArchiveEntry("./preinst"); preinstEntry.setSize(size); preinstEntry.setMode(0755); tar.putArchiveEntry(preinstEntry); tar.write(preinstBaseData); tar.closeArchiveEntry(); byte[] postinstBaseData = processTemplate("postinst", freemarkerConfig, config, combine("postinst.ftl", BASE_DIR + File.separator + "postinst", true)); size = postinstBaseData.length; TarArchiveEntry postinstEntry = new TarArchiveEntry("./postinst"); postinstEntry.setSize(size); postinstEntry.setMode(0755); tar.putArchiveEntry(postinstEntry); tar.write(postinstBaseData); tar.closeArchiveEntry(); byte[] prermBaseData = processTemplate("prerm", freemarkerConfig, config, combine("prerm.ftl", BASE_DIR + File.separator + "prerm", false)); size = prermBaseData.length; TarArchiveEntry prermEntry = new TarArchiveEntry("./prerm"); prermEntry.setSize(size); prermEntry.setMode(0755); tar.putArchiveEntry(prermEntry); tar.write(prermBaseData); tar.closeArchiveEntry(); byte[] postrmBaseData = processTemplate("postrm", freemarkerConfig, config, combine("postrm.ftl", BASE_DIR + File.separator + "postrm", false)); size = postrmBaseData.length; TarArchiveEntry postrmEntry = new TarArchiveEntry("./postrm"); postrmEntry.setSize(size); postrmEntry.setMode(0755); tar.putArchiveEntry(postrmEntry); tar.write(postrmBaseData); tar.closeArchiveEntry(); } catch (Exception e) { throw new MojoExecutionException("unable to create control tar", e); } finally { if (tar != null) { try { tar.close(); } catch (IOException e) { getLog().error("unable to finish tar", e); } } } }
From source file:lk.score.androphsy.main.NewCase.java
private void addFileToCompression(TarArchiveOutputStream taos, File f, String dir) throws IOException { System.out.println(f.getName() + " dir : " + dir); TarArchiveEntry tae = new TarArchiveEntry(f, dir); taos.putArchiveEntry(tae);/*from w w w . j av a2 s .c o m*/ if (f.isDirectory()) { System.out.println("is a directory"); taos.closeArchiveEntry(); for (File childFile : f.listFiles()) { System.out.println("child: " + childFile.getName()); addFileToCompression(taos, childFile, dir + "/" + childFile.getName()); } } else { System.out.println("is a file " + f.getName()); FileInputStream fis = new FileInputStream(f); IOUtils.copy(fis, taos); System.out.println("file added"); taos.flush(); taos.closeArchiveEntry(); } }
From source file:com.netflix.spinnaker.halyard.core.registry.v1.GitProfileReader.java
@Override public InputStream readArchiveProfile(String artifactName, String version, String profileName) throws IOException { Path profilePath = Paths.get(profilePath(artifactName, version, profileName)); ByteArrayOutputStream os = new ByteArrayOutputStream(); TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os); ArrayList<Path> filePathsToAdd = java.nio.file.Files .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS) .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new)); for (Path path : filePathsToAdd) { TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString()); int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path)); permissions = FileModeUtils.setFileBit(permissions); tarEntry.setMode(permissions);//from w w w .j av a 2 s . c om tarArchive.putArchiveEntry(tarEntry); IOUtils.copy(Files.newInputStream(path), tarArchive); tarArchive.closeArchiveEntry(); } tarArchive.finish(); tarArchive.close(); return new ByteArrayInputStream(os.toByteArray()); }
From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java
/** * There is an option to override the name of the first level entry if you want to pack * a directory. Set includeFolder = true so that it not only packs the contents but also * the containing folder. Then use the setter setFirstLevelEntryName and set the name * of the folder which contains the files to pack. The name of the folder then gets replaced * in the resulting tar. Note that after calling archiveFolder once, the variable gets automatically * reset so that you have to call the setter again if you want to set the override setting again. *//*from w ww. j av a 2s. c om*/ public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception { FileOutputStream fOut = null; BufferedOutputStream bOut = null; TarArchiveOutputStream tOut = null; fOut = new FileOutputStream(destFile); bOut = new BufferedOutputStream(fOut); tOut = new TarArchiveOutputStream(bOut); tOut.setLongFileMode(longFileMode); tOut.setBigNumberMode(bigNumberMode); try { String base = ""; if (firstLevelEntryName.isEmpty()) firstLevelEntryName = srcFolder.getName() + "/"; if (includeFolder) { logger.debug("addFileToTar: " + firstLevelEntryName); TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(srcFolder, firstLevelEntryName); tOut.putArchiveEntry(entry); tOut.closeArchiveEntry(); base = firstLevelEntryName; } File children[] = srcFolder.listFiles(); for (int i = 0; i < children.length; i++) { addFileToTar(tOut, children[i], base); } } finally { tOut.finish(); tOut.close(); bOut.close(); fOut.close(); firstLevelEntryName = ""; } }
From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java
private void sendImagesData(List<ImageDTO2> imageResults, TarArchiveOutputStream tos) throws IOException { InputStream dicomIn = null;/* w w w. jav a2s.co m*/ try { for (ImageDTO2 imageDto : imageResults) { String filePath = imageDto.getFileName(); String sop = imageDto.getSOPInstanceUID(); logger.info("filepath: " + filePath + " filename: " + sop); try { File dicomFile = new File(filePath); ArchiveEntry tarArchiveEntry = tos.createArchiveEntry(dicomFile, sop + ".dcm"); dicomIn = new FileInputStream(dicomFile); tos.putArchiveEntry(tarArchiveEntry); IOUtils.copy(dicomIn, tos); tos.closeArchiveEntry(); } catch (FileNotFoundException e) { e.printStackTrace(); // just print the exception and continue the loop so rest of // images will get download. } finally { IOUtils.closeQuietly(dicomIn); logger.info("DownloadServlet Image transferred at " + new Date().getTime()); } } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.moss.simpledeb.core.DebWriter.java
private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception { byte[] tarData; {/*from ww w . java 2s. c om*/ ByteArrayOutputStream tarOut = new ByteArrayOutputStream(); TarArchiveOutputStream tar = new TarArchiveOutputStream(tarOut); Set<String> writtenPaths = new HashSet<String>(); for (ArchivePath path : paths) { String name = path.entry().getName(); if (writtenPaths.contains(name)) { throw new RuntimeException("Duplicate archive entry: " + name); } writtenPaths.add(name); tar.putArchiveEntry(path.entry()); if (!path.entry().isDirectory()) { InputStream in = path.read(); byte[] buffer = new byte[1024 * 10]; for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) { tar.write(buffer, 0, numRead); } in.close(); } tar.closeArchiveEntry(); } tar.close(); tarData = tarOut.toByteArray(); } byte[] gzipData; { ByteArrayOutputStream gzipOut = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(gzipOut); gzip.write(tarData); gzip.close(); gzipData = gzipOut.toByteArray(); } return gzipData; }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java
/** * Encrypts and encodes the string and writes it to the tar output stream with the specified file name *///from www . j a v a2 s. c o m protected void encryptAndWrite(TarArchiveOutputStream tarArchiveOutputStream, String contents, String fileName) throws InvalidCipherTextException, IOException { final byte[] contentBytes = contents.getBytes(CHARSET); //Encrypt contents final AsymmetricBlockCipher encryptCipher = this.getEncryptCipher(); final byte[] encryptedContentBytes = encryptCipher.processBlock(contentBytes, 0, contentBytes.length); final byte[] encryptedContentBase64Bytes = Base64.encodeBase64(encryptedContentBytes); //Write encrypted contents to tar output stream final TarArchiveEntry contentEntry = new TarArchiveEntry(fileName); contentEntry.setSize(encryptedContentBase64Bytes.length); tarArchiveOutputStream.putArchiveEntry(contentEntry); tarArchiveOutputStream.write(encryptedContentBase64Bytes); tarArchiveOutputStream.closeArchiveEntry(); }