List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream flush
public void flush() throws IOException
From source file:big.zip.java
/** * /* w w w .j av a 2 s. c om*/ * @param fileToCompress The file that we want to compress * @param fileToOutput The zip file containing the compressed file * @return True if the file was compressed and created, false when something * went wrong */ public static boolean compress(final File fileToCompress, final File fileToOutput) { if (fileToOutput.exists()) { // do the first run to delete this file fileToOutput.delete(); // did this worked? if (fileToOutput.exists()) { // something went wrong, the file is still here System.out.println("ZIP59 - Failed to delete output file: " + fileToOutput.getAbsolutePath()); return false; } } // does our file to compress exist? if (fileToCompress.exists() == false) { // we have a problem here System.out.println("ZIP66 - Didn't found the file to compress: " + fileToCompress.getAbsolutePath()); return false; } // all checks are done, now it is time to do the compressing try { final OutputStream outputStream = new FileOutputStream(fileToOutput); ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream("zip", outputStream); archive.putArchiveEntry(new ZipArchiveEntry(fileToCompress.getName())); // create the input file stream and copy it over to the archive FileInputStream inputStream = new FileInputStream(fileToCompress); IOUtils.copy(inputStream, archive); // close the archive archive.closeArchiveEntry(); archive.flush(); archive.close(); // now close the input file stream inputStream.close(); // and close the output file stream too outputStream.flush(); outputStream.close(); } catch (FileNotFoundException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (ArchiveException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; }
From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java
/** * Lower level pack operation//from w w w. j a v a 2 s . c o m * * @param source * source file * @param destination * destination file * @return the number of bytes processed * @throws ArchiveException * @throws IOException */ public static long pack(String source, File destination) throws ArchiveException, IOException { ArchiveStreamFactory factory = new ArchiveStreamFactory(); OutputStream outStream = new FileOutputStream(destination); ArchiveOutputStream outputStream; try { outputStream = factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, outStream); } catch (ArchiveException e) { IOUtils.closeQuietly(outStream); throw e; } File startDirectory = new File(source); RecursiveArchiver archiver = new RecursiveArchiver(outputStream); try { archiver.archive(startDirectory); outputStream.flush(); outputStream.close(); outStream.flush(); outStream.close(); } catch (IOException e) { IOUtils.closeQuietly(outStream); IOUtils.closeQuietly(outputStream); throw e; } long ret = outputStream.getBytesWritten(); if (logger.isDebugEnabled()) { logger.debug("Packed " + ret + " bytes"); } return ret; }
From source file:big.BigZip.java
/** * Copies one file into the big archive * @param fileToCopy/*from ww w. ja v a 2 s.c o m*/ * @return */ public boolean writeFile(final File fileToCopy) { // declare ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream(); try { /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */ ArchiveOutputStream logical_zip = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream); /* Create Archieve entry - write header information*/ logical_zip.putArchiveEntry(new ZipArchiveEntry(fileToCopy.getName())); /* Copy input file */ IOUtils.copy(new FileInputStream(fileToCopy), logical_zip); logical_zip.closeArchiveEntry(); logical_zip.finish(); logical_zip.flush(); logical_zip.close(); // get the bytes final ByteArrayInputStream byteInput = new ByteArrayInputStream(outputZipStream.toByteArray()); byte[] buffer = new byte[8192]; int length, counter = 0; // add the magic number to this file block outputStream.write(magicSignature.getBytes()); // now copy the whole file into the BIG archive while ((length = byteInput.read(buffer)) > 0) { outputStream.write(buffer, 0, length); counter += length; } // if there is something else to be flushed, do it now outputStream.flush(); // calculate the base path final String resultingPath = fileToCopy.getAbsolutePath().replace(basePath, ""); // calculate the SHA1 signature final String output = utils.hashing.checksum.generateFileChecksum("SHA-1", fileToCopy); // write a new line in our index file writerFileIndex.write( "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + output + " " + resultingPath); // increase the position counter currentPosition += counter + magicSignature.length(); } catch (Exception e) { System.err.println("BIG346 - Error copying file: " + fileToCopy.getAbsolutePath()); return false; } finally { } return true; }
From source file:big.BigZip.java
/** * Copies one file into the big archive/* ww w .j a va 2s . c o m*/ * @param fileToCopy * @param SHA1 * @param filePathToWriteInTextLine * @return */ public boolean quickWrite(final File fileToCopy, final String SHA1, final String filePathToWriteInTextLine) { // declare ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream(); try { // save this operation on the log of commits addTagStarted(fileToCopy.getName()); //pointRestoreAndSave(fileToCopy); /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */ ArchiveOutputStream logical_zip = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream); /* Create Archieve entry - write header information*/ logical_zip.putArchiveEntry(new ZipArchiveEntry(fileToCopy.getName())); /* Copy input file */ IOUtils.copy(new FileInputStream(fileToCopy), logical_zip); logical_zip.closeArchiveEntry(); logical_zip.finish(); logical_zip.flush(); logical_zip.close(); // get the bytes final ByteArrayInputStream byteInput = new ByteArrayInputStream(outputZipStream.toByteArray()); byte[] buffer = new byte[8192]; int length, counter = 0; // add the magic number to this file block outputStream.write(magicSignature.getBytes()); // now copy the whole file into the BIG archive while ((length = byteInput.read(buffer)) > 0) { outputStream.write(buffer, 0, length); counter += length; } // if there is something else to be flushed, do it now //outputStream.flush(); // calculate the base path //final String resultingPath = fileToCopy.getAbsolutePath().replace(rootFolder, ""); final String line = "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + SHA1 + " " + filePathToWriteInTextLine; // write a new line in our index file writerFileIndex.write(line); //writer.flush(); // increase the position counter currentPosition += counter + magicSignature.length(); // close the log with success addTagEnded(); } catch (Exception e) { System.err.println("BIG600 - Error copying file: " + fileToCopy.getAbsolutePath()); return false; } finally { } return true; }
From source file:big.BigZip.java
/** * Copies one file into the big archive/*www. j a va 2 s. co m*/ * @param stream * @param SHA1 * @param filePathToWriteInTextLine * @return * @throws java.io.IOException */ public boolean quickWriteGenericStream(final InputStream stream, final String SHA1, final String filePathToWriteInTextLine) throws IOException { // declare ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream(); ByteArrayInputStream byteInput = null; try { // save this operation on the log of commits addTagStarted(filePathToWriteInTextLine); // Create Archive Output Stream that attaches File Output Stream / and specifies type of compression ArchiveOutputStream logical_zip = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream); // Create Archive entry - write header information ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(filePathToWriteInTextLine); logical_zip.putArchiveEntry(zipArchiveEntry); // Copy input file IOUtils.copy(stream, logical_zip); logical_zip.closeArchiveEntry(); logical_zip.finish(); logical_zip.flush(); logical_zip.close(); // get the bytes byteInput = new ByteArrayInputStream(outputZipStream.toByteArray()); byte[] buffer = new byte[8192]; int length, counter = 0; // add the magic number to this file block outputStream.write(magicSignature.getBytes()); // now copy the whole file into the BIG archive while ((length = byteInput.read(buffer)) > 0) { outputStream.write(buffer, 0, length); counter += length; } final String line = "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + SHA1 + " " + filePathToWriteInTextLine; // write a new line in our index file writerFileIndex.write(line); //writer.flush(); // increase the position counter currentPosition += counter + magicSignature.length(); // close the log with success addTagEnded(); } catch (Exception e) { System.err.println("BIG600 - Error copying file: " + filePathToWriteInTextLine); return false; } finally { if (byteInput != null) { byteInput.close(); } stream.close(); outputZipStream.close(); outputStream.close(); } return true; }
From source file:big.BigZip.java
/** * Requires an InputStream, it will calculate the SHA1 checksum at the same * time that it writes data onto the big file. The input stream is expected * to be closed outside of this method./*from w w w. jav a2s. com*/ * @param stream * @param filePathToWriteInTextLine * @throws java.io.IOException */ public void quickWriteStreamStandalone(final InputStream stream, final String filePathToWriteInTextLine) throws Exception { // declare ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream(); ByteArrayInputStream byteInput = null; // Create Archive Output Stream that attaches File Output Stream / and specifies type of compression ArchiveOutputStream logical_zip = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream); // Create Archive entry - write header information ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(filePathToWriteInTextLine); logical_zip.putArchiveEntry(zipArchiveEntry); // prepare the SHA1 signature generation final MessageDigest hash = MessageDigest.getInstance("SHA1"); // Copy input file byte[] buffer = new byte[16384]; int length; // decompress from the original zip file, compress to our zip format // calculate the SHA1 signature on the same loop to save resource while ((length = stream.read(buffer)) > 0) { logical_zip.write(buffer, 0, length); hash.update(buffer, 0, length); } // compute the file signature byte[] digest = hash.digest(); final String SHA1 = utils.hashing.checksum.convertHash(digest); // close the zip related objects logical_zip.closeArchiveEntry(); logical_zip.finish(); logical_zip.flush(); logical_zip.close(); logical_zip = null; // define the line that will be written on the index file final String line = "\n".concat(utils.files.getPrettyFileSize(currentPosition)).concat(" ").concat(SHA1) .concat(" ").concat(filePathToWriteInTextLine); // get the bytes byteInput = new ByteArrayInputStream(outputZipStream.toByteArray()); int counter = 0; // add the magic number to this file block outputStream.write(magicSignature.getBytes()); // now copy the whole file into the BIG archive while ((length = byteInput.read(buffer)) > 0) { outputStream.write(buffer, 0, length); counter += length; } // write a new line in our index file writerFileIndex.write(line); // increase the position counter currentPosition += counter + magicSignature.length(); // close the streams that were created byteInput.close(); outputZipStream.close(); }
From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractExecWarMojo.java
/** * return file can be deleted/*from w w w. j a va 2 s . c om*/ */ protected File addContextXmlToWar(File contextXmlFile, File warFile) throws IOException, ArchiveException { ArchiveOutputStream os = null; OutputStream warOutputStream = null; File tmpWar = File.createTempFile("tomcat", "war-exec"); tmpWar.deleteOnExit(); try { warOutputStream = new FileOutputStream(tmpWar); os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, warOutputStream); os.putArchiveEntry(new JarArchiveEntry("META-INF/context.xml")); IOUtils.copy(new FileInputStream(contextXmlFile), os); os.closeArchiveEntry(); JarFile jarFile = new JarFile(warFile); extractJarToArchive(jarFile, os, null); os.flush(); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(warOutputStream); } return tmpWar; }
From source file:org.deventropy.shared.utils.DirectoryArchiverUtil.java
private static void createArchiveOfDirectory(final String archiveFile, final File srcDirectory, final String rootPathPrefix, final String archiveStreamFactoryConstant, final String encoding, final ArchiverCreateProcessor archiverCreateProcessorIn) throws IOException { /*/*from w ww . jav a 2s . c o m*/ * NOTE ON CHARSET ENCODING: Traditionally the ZIP archive format uses CodePage 437 as encoding for file name, * which is not sufficient for many international character sets. * Over time different archivers have chosen different ways to work around the limitation - the java.util.zip * packages simply uses UTF-8 as its encoding for example. * Ant has been offering the encoding attribute of the zip and unzip task as a way to explicitly specify the * encoding to use (or expect) since Ant 1.4. It defaults to the platform's default encoding for zip and UTF-8 * for jar and other jar-like tasks (war, ear, ...) as well as the unzip family of tasks. */ final ArchiverCreateProcessor archiveCreateProcessor = (null != archiverCreateProcessorIn) ? archiverCreateProcessorIn : new ArchiverCreateProcessor(); ArchiveOutputStream aos = null; try { final ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(encoding); final FileOutputStream archiveFileOutputStream = new FileOutputStream(archiveFile); final OutputStream decoratedArchiveFileOutputStream = archiveCreateProcessor .decorateFileOutputStream(archiveFileOutputStream); aos = archiveStreamFactory.createArchiveOutputStream(archiveStreamFactoryConstant, decoratedArchiveFileOutputStream); archiveCreateProcessor.processArchiverPostCreate(aos, encoding); final String normalizedRootPathPrefix = (null == rootPathPrefix || rootPathPrefix.isEmpty()) ? "" : normalizeName(rootPathPrefix, true); if (!normalizedRootPathPrefix.isEmpty()) { final ArchiveEntry archiveEntry = aos.createArchiveEntry(srcDirectory, normalizedRootPathPrefix); aos.putArchiveEntry(archiveEntry); aos.closeArchiveEntry(); } final Path srcRootPath = Paths.get(srcDirectory.toURI()); final ArchiverFileVisitor visitor = new ArchiverFileVisitor(srcRootPath, normalizedRootPathPrefix, aos); Files.walkFileTree(srcRootPath, visitor); aos.flush(); } catch (ArchiveException e) { throw new IOException("Error creating archive", e); } finally { if (null != aos) { aos.close(); } } }
From source file:org.jwebsocket.util.Tools.java
/** * Compress a byte array using zip compression * * @param aBA/*from w ww. j a va 2 s. c om*/ * @param aBase64Encode if TRUE, the result is Base64 encoded * @return * @throws Exception */ public static byte[] zip(byte[] aBA, Boolean aBase64Encode) throws Exception { ByteArrayOutputStream lBAOS = new ByteArrayOutputStream(); ArchiveOutputStream lAOS = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, lBAOS); ZipArchiveEntry lZipEntry = new ZipArchiveEntry("temp.zip"); lZipEntry.setSize(aBA.length); lAOS.putArchiveEntry(lZipEntry); lAOS.write(aBA); lAOS.closeArchiveEntry(); lAOS.flush(); lAOS.close(); if (aBase64Encode) { aBA = Base64.encodeBase64(lBAOS.toByteArray()); } else { aBA = lBAOS.toByteArray(); } return aBA; }
From source file:org.rauschig.jarchivelib.CommonsArchiver.java
@Override public File create(String archive, File destination, File... sources) throws IOException { IOUtils.requireDirectory(destination); File archiveFile = createNewArchiveFile(archive, getFilenameExtension(), destination); ArchiveOutputStream outputStream = null; try {/*from w ww. j a v a2 s . co m*/ outputStream = createArchiveOutputStream(archiveFile); writeToArchive(sources, outputStream); outputStream.flush(); } finally { IOUtils.closeQuietly(outputStream); } return archiveFile; }