List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
From source file:org.hyperledger.fabric.sdkintegration.Util.java
/** * Generate a targz inputstream from source folder. * * @param src Source location//from www .j a v a 2 s.com * @param pathPrefix prefix to add to the all files found. * @return return inputstream. * @throws IOException */ public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException { File sourceDirectory = src; ByteArrayOutputStream bos = new ByteArrayOutputStream(500000); String sourcePath = sourceDirectory.getAbsolutePath(); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(bos))); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); if (pathPrefix != null) { relativePath = Utils.combinePaths(pathPrefix, relativePath); } relativePath = FilenameUtils.separatorsToUnix(relativePath); archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } } finally { IOUtils.closeQuietly(archiveOutputStream); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.icgc.dcc.download.client.io.ArchiveOutputStream.java
@SneakyThrows private static void closeArchiveEntry(TarArchiveOutputStream os) { os.closeArchiveEntry(); }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, String archivePath) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); File tarFile = new File(archivePath); String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/")))); byte[] buf = new byte[1024]; int len;//from ww w .j av a 2 s .c o m TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); byte[] buf = new byte[1024]; int len;/*from www .j a v a 2 s .c om*/ TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try {// w ww .j a v a 2 s . com for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { Files.asByteSource(file).copyTo(tos); } tos.closeArchiveEntry(); } } finally { tos.close(); } return tarFile; }
From source file:org.jenkinsci.plugins.os_ci.utils.CompressUtils.java
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException { File f = new File(path); String entryName = base + f.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); tOut.putArchiveEntry(tarEntry);/* w w w.java2 s . co m*/ if (f.isFile()) { IOUtils.copy(new FileInputStream(f), tOut); tOut.closeArchiveEntry(); } else { tOut.closeArchiveEntry(); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/"); } } } }
From source file:org.kitesdk.cli.commands.TestTarImportCommand.java
private static void writeToTarFile(TarArchiveOutputStream tos, String name, String content) throws IOException { TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name); if (null != content) { tarArchiveEntry.setSize(content.length()); }/*from ww w . j ava2 s. c om*/ tarArchiveEntry.setModTime(System.currentTimeMillis()); tos.putArchiveEntry(tarArchiveEntry); if (null != content) { byte[] buf = content.getBytes(); tos.write(buf, 0, content.length()); tos.flush(); } tos.closeArchiveEntry(); }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add a folder into tar./*from w ww.j a v a2s . c om*/ * * @param tarStream TarArchive outputStream * @param path path to append * @throws IOException thrown when having IO problem. */ public static void addFolderToTar(TarArchiveOutputStream tarStream, String path) throws IOException { TarArchiveEntry archiveEntry = new TarArchiveEntry(path); archiveEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); tarStream.putArchiveEntry(archiveEntry); tarStream.closeArchiveEntry(); }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add the given input stream into tar./*ww w . j a v a 2 s . c om*/ * * @param tarStream TarArchive outputStream * @param inputStream input stream * @param path relative path to append * @param size size of stream * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addInputStreamToTar(TarArchiveOutputStream tarStream, InputStream inputStream, String path, long size, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(size); entry.setMode(mode); try { tarStream.putArchiveEntry(entry); IOUtils.copy(inputStream, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { tarStream.closeArchiveEntry(); } }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add a file into tar.//from w w w . j a v a2s . c o m * * @param tarStream TarArchive outputStream * @param file file * @param path relative path to append * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addFileToTar(TarArchiveOutputStream tarStream, File file, String path, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(file.length()); entry.setMode(mode); BufferedInputStream bis = null; try { tarStream.putArchiveEntry(entry); bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { IOUtils.closeQuietly(bis); tarStream.closeArchiveEntry(); } }