List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream putArchiveEntry
public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException
From source file:com.salsaberries.narchiver.Writer.java
private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException { // Create an entry for the file 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(bis, taos);//from ww w . j a v a 2 s . com taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // Close the archive entry 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.getName()); } } }
From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java
private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException { // Create an entry for the file 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(bis, taos);/* w ww. j a va 2 s.c om*/ taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // close the archive entry 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, dir + File.separator + file.getName()); } } }
From source file:com.pinterest.deployservice.common.TarUtils.java
static void addInputStreamToTar(TarArchiveOutputStream taos, InputStream is, String path, long size, int mode) throws Exception { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(size);// ww w . j a v a2 s .c o m entry.setMode(mode); try { taos.putArchiveEntry(entry); IOUtils.copy(is, taos); } finally { taos.closeArchiveEntry(); Closeables.closeQuietly(is); } }
From source file:company.gonapps.loghut.utils.FileUtils.java
private static void addFileToArchive(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) throws IOException { File file = new File(path); String entryName = base + file.getName(); tarArchiveOutputStream.putArchiveEntry(new TarArchiveEntry(file, entryName)); if (file.isFile()) { try (FileInputStream fileInputStream = new FileInputStream(file)) { IOUtils.copy(fileInputStream, tarArchiveOutputStream); }/* w ww .j a va 2s .c o m*/ tarArchiveOutputStream.closeArchiveEntry(); } else { tarArchiveOutputStream.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) addFileToArchive(tarArchiveOutputStream, child.getAbsolutePath(), entryName + "/"); } } }
From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java
private static void addFileToTarGz(TarArchiveOutputStream tOut, Path path, String base) throws IOException { File f = path.toFile();//from www .j ava 2 s . c o m String entryName = base + f.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); tOut.putArchiveEntry(tarEntry); 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.toPath().toAbsolutePath(), entryName + "/"); } } } }
From source file:msec.org.TarUtil.java
private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception { TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName()); entry.setSize(file.length());//from ww w. j a v a2s .co m taos.putArchiveEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int count; byte data[] = new byte[BUFFERSZ]; while ((count = bis.read(data, 0, BUFFERSZ)) != -1) { taos.write(data, 0, count); } bis.close(); taos.closeArchiveEntry(); }
From source file:com.github.trask.comet.loadtest.aws.ChefBootstrap.java
private static void addToTarWithBase(File file, TarArchiveOutputStream tarOut, String base) throws IOException { String entryName = base + file.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName); tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tarOut.putArchiveEntry(tarEntry); if (file.isFile()) { IOUtils.copy(new FileInputStream(file), tarOut); tarOut.closeArchiveEntry();/*from www . j a va2s . co m*/ } else { tarOut.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) { addToTarWithBase(child, tarOut, entryName + "/"); } } } }
From source file:com.vmware.admiral.closures.util.ClosureUtils.java
private static void putTarEntry(TarArchiveOutputStream tarOutputStream, TarArchiveEntry tarEntry, InputStream inStream, long size) throws IOException { tarEntry.setSize(size);/*w w w . j av a 2s.c om*/ tarOutputStream.putArchiveEntry(tarEntry); try (InputStream input = new BufferedInputStream(inStream)) { long byteRead = copy(input, tarOutputStream); logInfo("---- BYTES READ %s ", byteRead); tarOutputStream.closeArchiveEntry(); } }
From source file:com.ipcglobal.fredimport.util.FredUtils.java
/** * Creates a tar entry for the path specified with a name built from the base passed in and the file/directory name. If the path is a directory, a recursive * call is made such that the full directory is added to the tar. * * @param tOut//from w w w . ja va 2 s.co m * The tar file's output stream * @param path * The filesystem path of the file/directory being added * @param base * The base prefix to for the name of the tar file entry * * @throws IOException * If anything goes wrong */ private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException { try { File f = new File(path); String entryName = base + f.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); tOut.putArchiveEntry(tarEntry); if (f.isFile()) { // had to do this in 3 steps due to memory leak as per http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress/23524963#23524963 FileInputStream in = new FileInputStream(f); IOUtils.copy(in, tOut); in.close(); tOut.closeArchiveEntry(); } else { tOut.closeArchiveEntry(); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/"); } } } } catch (Exception e) { System.out.println(e); e.printStackTrace(); throw e; } }
From source file:msec.org.TarUtil.java
private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception { File[] files = dir.listFiles(); if (files.length < 1) { TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + File.separator); taos.putArchiveEntry(entry); taos.closeArchiveEntry();/*w ww .j a va 2 s . c o m*/ } for (File file : files) { // archive(file, taos, basePath + dir.getName() + File.separator); } }