List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream write
public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException
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());/* w w w . j a v a2s . c om*/ 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.codenvy.commons.lang.TarUtils.java
private static void addFileEntry(TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException { final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName); if (modTime >= 0) { tarEntry.setModTime(modTime);/*from w ww.j a va 2 s . c om*/ } tarOut.putArchiveEntry(tarEntry); try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { final byte[] buf = new byte[BUF_SIZE]; int r; while ((r = in.read(buf)) != -1) { tarOut.write(buf, 0, r); } } tarOut.closeArchiveEntry(); }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param basePath/*from ww w . j a v a 2 s.c om*/ * @param tarPath * @param filePaths * @throws java.io.IOException */ public static void tar(String basePath, String tarPath, Map<String, String> filePaths) throws IOException { BufferedInputStream origin = null; TarArchiveOutputStream out = null; FileOutputStream dest = null; try { dest = new FileOutputStream(tarPath); out = new TarArchiveOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; for (Map.Entry<String, String> entryFile : filePaths.entrySet()) { String filename = entryFile.getKey(); String filePath = entryFile.getValue(); System.out.println("Adding: " + filename + " => " + filePath); FileInputStream fi = new FileInputStream(basePath + filePath); origin = new BufferedInputStream(fi, BUFFER); TarArchiveEntry entry = new TarArchiveEntry(filename); out.putArchiveEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } } catch (Exception e) { e.printStackTrace(); } finally { if (origin != null) origin.close(); if (out != null) out.close(); if (dest != null) dest.close(); } }
From source file:com.moss.simpledeb.core.DebWriter.java
private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception { byte[] tarData; {/*from ww w.ja va2s. com*/ 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:adams.core.io.TarUtils.java
/** * Creates a tar file from the specified files. * <br><br>/*from www .jav a2 s . co m*/ * See <a href="http://www.thoughtspark.org/node/53" target="_blank">Creating a tar.gz with commons-compress</a>. * * @param output the output file to generate * @param files the files to store in the tar file * @param stripRegExp the regular expression used to strip the file names * @param bufferSize the buffer size to use * @return null if successful, otherwise error message */ @MixedCopyright(author = "Jeremy Whitlock (jcscoobyrs)", copyright = "2010 Jeremy Whitlock", license = License.APACHE2, url = "http://www.thoughtspark.org/node/53") public static String compress(File output, File[] files, String stripRegExp, int bufferSize) { String result; int i; byte[] buf; int len; TarArchiveOutputStream out; BufferedInputStream in; FileInputStream fis; FileOutputStream fos; String filename; String msg; TarArchiveEntry entry; in = null; fis = null; out = null; fos = null; result = null; try { // does file already exist? if (output.exists()) System.err.println("WARNING: overwriting '" + output + "'!"); // create tar file buf = new byte[bufferSize]; fos = new FileOutputStream(output.getAbsolutePath()); out = openArchiveForWriting(output, fos); for (i = 0; i < files.length; i++) { fis = new FileInputStream(files[i].getAbsolutePath()); in = new BufferedInputStream(fis); // Add tar entry to output stream. filename = files[i].getParentFile().getAbsolutePath(); if (stripRegExp.length() > 0) filename = filename.replaceFirst(stripRegExp, ""); if (filename.length() > 0) filename += File.separator; filename += files[i].getName(); entry = new TarArchiveEntry(filename); if (files[i].isFile()) entry.setSize(files[i].length()); out.putArchiveEntry(entry); // Transfer bytes from the file to the tar file while ((len = in.read(buf)) > 0) out.write(buf, 0, len); // Complete the entry out.closeArchiveEntry(); FileUtils.closeQuietly(in); FileUtils.closeQuietly(fis); in = null; fis = null; } // Complete the tar file FileUtils.closeQuietly(out); FileUtils.closeQuietly(fos); out = null; fos = null; } catch (Exception e) { msg = "Failed to generate archive '" + output + "': "; System.err.println(msg); e.printStackTrace(); result = msg + e; } finally { FileUtils.closeQuietly(in); FileUtils.closeQuietly(fis); FileUtils.closeQuietly(out); FileUtils.closeQuietly(fos); } return result; }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackager.java
private void copyFileToArchive(final TarArchiveOutputStream out, final String tempFilename, final String filename) throws IOException { if (StringUtils.isEmpty(tempFilename)) { return;// w w w . j a va 2s. co m } final byte[] buffer = new byte[1024]; final File file = new File(tempFilename); if (!file.exists()) { throw new IOException("File does not exist: " + tempFilename); } final TarArchiveEntry tarAdd = new TarArchiveEntry(file); tarAdd.setModTime(file.lastModified()); tarAdd.setName(filename); out.putArchiveEntry(tarAdd); FileInputStream in = null; try { in = new FileInputStream(file); int nRead = in.read(buffer, 0, buffer.length); while (nRead >= 0) { out.write(buffer, 0, nRead); nRead = in.read(buffer, 0, buffer.length); } } finally { if (in != null) { in.close(); } } out.closeArchiveEntry(); }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackager.java
void makeArchive(final String readmeTempFilename) throws IOException { final byte[] buffer = new byte[1024]; File archiveFile = null;//from w w w .j a v a 2 s .c om TarArchiveOutputStream out = null; //in case we need to write to an external server final String archiveName = prefixPathForExternalServer( filePackagerBean.getArchivePhysicalName() + ".tar.gz"); try { archiveFile = new File(archiveName); out = makeTarGzOutputStream(archiveFile); copyManifestToArchive(out); copyReadmeToArchive(out, readmeTempFilename); int i = 0; for (final DataFile fileInfo : filePackagerBean.getSelectedFiles()) { final File file = new File(fileInfo.getPath()); if (!file.exists()) { throw new IOException("Data file does not exist: " + fileInfo.getPath()); } logger.logToLogger(Level.DEBUG, "tarring file " + (++i) + ":" + fileInfo.getPath() + " into " + archiveName); //"synthetic" file path, as we want it to appear in the tar final String archiveFilePath = constructInternalFilePath(fileInfo); final TarArchiveEntry tarAdd = new TarArchiveEntry(file); tarAdd.setModTime(file.lastModified()); tarAdd.setName(archiveFilePath); out.putArchiveEntry(tarAdd); FileInputStream in = null; try { in = new FileInputStream(file); int nRead = in.read(buffer, 0, buffer.length); while (nRead >= 0) { out.write(buffer, 0, nRead); nRead = in.read(buffer, 0, buffer.length); } } finally { if (in != null) { in.close(); } } out.closeArchiveEntry(); if (fileInfo.getCacheFileToGenerate() != null) { //a special case where there should be a cache file but it doesn't exist - // Send email with error message //filePackagerFactory.getErrorMailSender().send(Messages.CACHE_ERROR, MessageFormat.format(Messages.CACHE_FILE_NOT_FOUND, fileInfo.getCacheFileToGenerate())); } } } catch (IOException ex) { //delete the out file if it exists if (out != null) { out.close(); out = null; } if (archiveFile != null && archiveFile.exists()) { // give OS time to delete file handle try { Thread.sleep(100); } catch (InterruptedException ie) { // it's ok } // keep track of uncompressed size this.actualUncompressedSize = archiveFile.length(); //noinspection ResultOfMethodCallIgnored archiveFile.delete(); } throw ex; } finally { if (out != null) { out.close(); } } logger.logToLogger(Level.DEBUG, "Created tar " + archiveName); }
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * Perform file compression.//from ww w . jav a 2s. co m * * @param inFileName * Name of the file to be compressed * @throws IOException */ public static String doCompressFile(String inFileName, String outFileName) throws IOException { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "doCompressFile", "[inFileName:" + inFileName + "]")); FileOutputStream fOut = null; BufferedOutputStream bOut = null; GzipCompressorOutputStream gzOut = null; TarArchiveOutputStream tOut = null; if (StringUtils.isEmpty(inFileName)) { throw new IllegalArgumentException(); } String compressedFileName = outFileName; FileInputStream fi = null; BufferedInputStream sourceStream = null; try { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_COMPRESS_PROCESS, "Creating the GZIP output stream")); /** Step: 1 ---> create a TarArchiveOutputStream object. **/ fOut = new FileOutputStream(new File(compressedFileName)); bOut = new BufferedOutputStream(fOut); gzOut = new GzipCompressorOutputStream(bOut); tOut = new TarArchiveOutputStream(gzOut); /** * Step: 2 --->Open the source data and get a list of files from * given directory. */ File source = new File(inFileName); if (!source.exists()) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_COMPRESS_ERROR, "File not found. " + inFileName)); return null; } File[] files = null; if (source.isDirectory()) { files = source.listFiles(); } else { files = new File[1]; files[0] = source; } for (int i = 0; i < files.length; i++) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_COMPRESS_PROCESS, "Adding File:" + source.getParentFile().toURI().relativize(files[i].toURI()).getPath())); /** * Step: 3 ---> Create a tar entry for each file that is read. */ /** * relativize is used to to add a file to a tar, without * including the entire path from root. */ TarArchiveEntry entry = new TarArchiveEntry(files[i], source.getParentFile().toURI().relativize(files[i].toURI()).getPath()); /** * Step: 4 ---> Put the tar entry using putArchiveEntry. */ tOut.putArchiveEntry(entry); /** * Step: 5 ---> Write the data to the tar file and close the * input stream. */ fi = new FileInputStream(files[i]); sourceStream = new BufferedInputStream(fi, TesseractConstant.FILE_BLOCK_SIZE); int count; byte[] data = new byte[TesseractConstant.FILE_BLOCK_SIZE]; while ((count = sourceStream.read(data, 0, TesseractConstant.FILE_BLOCK_SIZE)) != -1) { tOut.write(data, 0, count); } sourceStream.close(); /** * Step: 6 --->close the archive entry. */ tOut.closeArchiveEntry(); } /** * Step: 7 --->close the output stream. */ tOut.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_COMPRESS_ERROR, "IOException")); LOGGER.error(e.getMessage(), e); throw e; } finally { try { fOut.close(); bOut.close(); gzOut.close(); tOut.close(); fi.close(); sourceStream.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_COMPRESS_ERROR, "IOException occur when closing fd")); LOGGER.error(e.getMessage(), e); throw e; } } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "doCompressFile", "[inFileName:" + inFileName + "][compressedFileName:" + compressedFileName + "]")); return compressedFileName; }
From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java
/** * This makes a .tgz or .tar.gz file./*from w w w .ja v a 2 s. co m*/ * * @param tResultName is the full result file name, usually * the name of the dir being archived, and ending in .tgz or .tar.gz. */ public static void makeTgz(String tDir, String tFileNameRegex, boolean tRecursive, String tPathRegex, String tResultName) throws Exception { TarArchiveOutputStream tar = null; String outerDir = File2.getDirectory(tDir.substring(0, tDir.length() - 1)); tar = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tResultName)))); // Add data to out and flush stream Table filesTable = oneStep(tDir, tFileNameRegex, tRecursive, tPathRegex, false); //tDirectoriesToo StringArray directoryPA = (StringArray) filesTable.getColumn(DIRECTORY); StringArray namePA = (StringArray) filesTable.getColumn(NAME); LongArray lastModifiedPA = (LongArray) filesTable.getColumn(LASTMODIFIED); LongArray sizePA = (LongArray) filesTable.getColumn(SIZE); byte buffer[] = new byte[32768]; int nBytes; for (int fi = 0; fi < namePA.size(); fi++) { String fullName = directoryPA.get(fi) + namePA.get(fi); TarArchiveEntry entry = new TarArchiveEntry(new File(fullName.substring(outerDir.length()))); entry.setSize(sizePA.get(fi)); entry.setModTime(lastModifiedPA.get(fi)); tar.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fullName); while ((nBytes = fis.read(buffer)) > 0) tar.write(buffer, 0, nBytes); fis.close(); tar.closeArchiveEntry(); } tar.close(); }
From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java
private static void addToZip(File directoryToZip, File file, TarArchiveOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); String filePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length()); Bukkit.getLogger().info(filePath);// w w w.ja va2 s . c o m ArchiveEntry zipEntry = zos.createArchiveEntry(file, filePath); zos.putArchiveEntry(zipEntry); final byte[] buf = new byte[8192]; int bytesRead; while (-1 != (bytesRead = fis.read(buf))) zos.write(buf, 0, bytesRead); zos.closeArchiveEntry(); fis.close(); }