List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream close
public void close() throws IOException
From source file:cz.muni.fi.xklinec.zipstream.App.java
/** * Entry point. /*from w ww . j ava 2 s. c om*/ * * @param args * @throws FileNotFoundException * @throws IOException * @throws NoSuchFieldException * @throws ClassNotFoundException * @throws NoSuchMethodException */ public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, InterruptedException { OutputStream fos = null; InputStream fis = null; if ((args.length != 0 && args.length != 2)) { System.err.println(String.format("Usage: app.jar source.apk dest.apk")); return; } else if (args.length == 2) { System.err.println( String.format("Will use file [%s] as input file and [%s] as output file", args[0], args[1])); fis = new FileInputStream(args[0]); fos = new FileOutputStream(args[1]); } else if (args.length == 0) { System.err.println(String.format("Will use file [STDIN] as input file and [STDOUT] as output file")); fis = System.in; fos = System.out; } final Deflater def = new Deflater(9, true); ZipArchiveInputStream zip = new ZipArchiveInputStream(fis); // List of postponed entries for further "processing". List<PostponedEntry> peList = new ArrayList<PostponedEntry>(6); // Output stream ZipArchiveOutputStream zop = new ZipArchiveOutputStream(fos); zop.setLevel(9); // Read the archive ZipArchiveEntry ze = zip.getNextZipEntry(); while (ze != null) { ZipExtraField[] extra = ze.getExtraFields(true); byte[] lextra = ze.getLocalFileDataExtra(); UnparseableExtraFieldData uextra = ze.getUnparseableExtraFieldData(); byte[] uextrab = uextra != null ? uextra.getLocalFileDataData() : null; // ZipArchiveOutputStream.DEFLATED // // Data for entry byte[] byteData = Utils.readAll(zip); byte[] deflData = new byte[0]; int infl = byteData.length; int defl = 0; // If method is deflated, get the raw data (compress again). if (ze.getMethod() == ZipArchiveOutputStream.DEFLATED) { def.reset(); def.setInput(byteData); def.finish(); byte[] deflDataTmp = new byte[byteData.length * 2]; defl = def.deflate(deflDataTmp); deflData = new byte[defl]; System.arraycopy(deflDataTmp, 0, deflData, 0, defl); } System.err.println(String.format( "ZipEntry: meth=%d " + "size=%010d isDir=%5s " + "compressed=%07d extra=%d lextra=%d uextra=%d " + "comment=[%s] " + "dataDesc=%s " + "UTF8=%s " + "infl=%07d defl=%07d " + "name [%s]", ze.getMethod(), ze.getSize(), ze.isDirectory(), ze.getCompressedSize(), extra != null ? extra.length : -1, lextra != null ? lextra.length : -1, uextrab != null ? uextrab.length : -1, ze.getComment(), ze.getGeneralPurposeBit().usesDataDescriptor(), ze.getGeneralPurposeBit().usesUTF8ForNames(), infl, defl, ze.getName())); final String curName = ze.getName(); // META-INF files should be always on the end of the archive, // thus add postponed files right before them if (curName.startsWith("META-INF") && peList.size() > 0) { System.err.println( "Now is the time to put things back, but at first, I'll perform some \"facelifting\"..."); // Simulate som evil being done Thread.sleep(5000); System.err.println("OK its done, let's do this."); for (PostponedEntry pe : peList) { System.err.println( "Adding postponed entry at the end of the archive! deflSize=" + pe.deflData.length + "; inflSize=" + pe.byteData.length + "; meth: " + pe.ze.getMethod()); pe.dump(zop, false); } peList.clear(); } // Capturing interesting files for us and store for later. // If the file is not interesting, send directly to the stream. if ("classes.dex".equalsIgnoreCase(curName) || "AndroidManifest.xml".equalsIgnoreCase(curName)) { System.err.println("### Interesting file, postpone sending!!!"); PostponedEntry pe = new PostponedEntry(ze, byteData, deflData); peList.add(pe); } else { // Write ZIP entry to the archive zop.putArchiveEntry(ze); // Add file data to the stream zop.write(byteData, 0, infl); zop.closeArchiveEntry(); } ze = zip.getNextZipEntry(); } // Cleaning up stuff zip.close(); fis.close(); zop.finish(); zop.close(); fos.close(); System.err.println("THE END!"); }
From source file:au.org.ala.biocache.util.AlaFileUtils.java
/** * Creates a zip file at the specified path with the contents of the specified directory. * NB://from w w w . ja v a 2 s . c o m * * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip * @throws IOException If anything goes wrong */ public static void createZip(String directoryPath, String zipPath) throws IOException { FileOutputStream fOut = null; BufferedOutputStream bOut = null; ZipArchiveOutputStream tOut = null; try { fOut = new FileOutputStream(new File(zipPath)); bOut = new BufferedOutputStream(fOut); tOut = new ZipArchiveOutputStream(bOut); addFileToZip(tOut, directoryPath, ""); } finally { tOut.finish(); tOut.close(); bOut.close(); fOut.close(); } }
From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java
public static void writeArchivedLogTailToStream(File logFile, OutputStream outputStream) throws IOException { if (!logFile.exists()) { throw new FileNotFoundException(); }//from w w w.j a v a 2 s.c o m ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); zipOutputStream.setEncoding(ZIP_ENCODING); byte[] content = getTailBytes(logFile); ArchiveEntry archiveEntry = newTailArchive(logFile.getName(), content); zipOutputStream.putArchiveEntry(archiveEntry); zipOutputStream.write(content); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); }
From source file:com.github.braully.graph.DatabaseFacade.java
public static void allResultsZiped(OutputStream out) throws IOException { ZipArchiveOutputStream tOut = null; try {//from w ww. ja v a2s .c o m tOut = new ZipArchiveOutputStream(out); addFileToZip(tOut, DATABASE_DIRECTORY, ""); } finally { tOut.flush(); tOut.close(); } }
From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java
public static void writeArchivedLogToStream(File logFile, OutputStream outputStream) throws IOException { if (!logFile.exists()) { throw new FileNotFoundException(); }/*from www . ja va 2s. c om*/ File tempFile = File.createTempFile(FilenameUtils.getBaseName(logFile.getName()) + "_log_", ".zip"); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFile); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); zipOutputStream.setEncoding(ZIP_ENCODING); ArchiveEntry archiveEntry = newArchive(logFile); zipOutputStream.putArchiveEntry(archiveEntry); FileInputStream logFileInput = new FileInputStream(logFile); IOUtils.copyLarge(logFileInput, zipOutputStream); logFileInput.close(); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); FileInputStream tempFileInput = new FileInputStream(tempFile); IOUtils.copyLarge(tempFileInput, outputStream); tempFileInput.close(); FileUtils.deleteQuietly(tempFile); }
From source file:com.ikon.util.ArchiveUtils.java
/** * Recursively create ZIP archive from directory *//* www .j a va2 s.co m*/ public static void createZip(File path, String root, OutputStream os) throws IOException { log.debug("createZip({}, {}, {})", new Object[] { path, root, os }); if (path.exists() && path.canRead()) { ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os); zaos.setComment("Generated by openkm"); zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS); zaos.setUseLanguageEncodingFlag(true); zaos.setFallbackToUTF8(true); zaos.setEncoding("UTF-8"); // Prevents java.util.zip.ZipException: ZIP file must have at least one entry ZipArchiveEntry zae = new ZipArchiveEntry(root + "/"); zaos.putArchiveEntry(zae); zaos.closeArchiveEntry(); createZipHelper(path, zaos, root); zaos.flush(); zaos.finish(); zaos.close(); } else { throw new IOException("Can't access " + path); } log.debug("createZip: void"); }
From source file:com.openkm.util.ArchiveUtils.java
/** * Recursively create ZIP archive from directory */// w ww. j ava 2s . c o m public static void createZip(File path, String root, OutputStream os) throws IOException { log.debug("createZip({}, {}, {})", new Object[] { path, root, os }); if (path.exists() && path.canRead()) { ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os); zaos.setComment("Generated by OpenKM"); zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS); zaos.setUseLanguageEncodingFlag(true); zaos.setFallbackToUTF8(true); zaos.setEncoding("UTF-8"); // Prevents java.util.zip.ZipException: ZIP file must have at least one entry ZipArchiveEntry zae = new ZipArchiveEntry(root + "/"); zaos.putArchiveEntry(zae); zaos.closeArchiveEntry(); createZipHelper(path, zaos, root); zaos.flush(); zaos.finish(); zaos.close(); } else { throw new IOException("Can't access " + path); } log.debug("createZip: void"); }
From source file:com.openkm.util.ArchiveUtils.java
/** * Create ZIP archive from file//from w w w . j a va2 s . com */ public static void createZip(File path, OutputStream os) throws IOException { log.debug("createZip({}, {})", new Object[] { path, os }); if (path.exists() && path.canRead()) { ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os); zaos.setComment("Generated by OpenKM"); zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS); zaos.setUseLanguageEncodingFlag(true); zaos.setFallbackToUTF8(true); zaos.setEncoding("UTF-8"); log.debug("FILE {}", path); ZipArchiveEntry zae = new ZipArchiveEntry(path.getName()); zaos.putArchiveEntry(zae); FileInputStream fis = new FileInputStream(path); IOUtils.copy(fis, zaos); fis.close(); zaos.closeArchiveEntry(); zaos.flush(); zaos.finish(); zaos.close(); } else { throw new IOException("Can't access " + path); } log.debug("createZip: void"); }
From source file:fr.acxio.tools.agia.tasks.ZipFilesTasklet.java
@Override public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception { // 1. Destination exists // a. Overwrite => default behaviour // b. Update => copy to temporary file, open, read entries, merge with new entries, write merged entries and stream // 2. New destination => default behaviour Map<String, Object> aSourceParams = new HashMap<String, Object>(); aSourceParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC, ((sChunkContext != null) && (sChunkContext.getStepContext() != null)) ? sChunkContext.getStepContext().getStepExecution() : null);//from w w w. j a v a2s . c om aSourceParams.put(ResourceFactoryConstants.PARAM_BASE_DIRECTORY, sourceBaseDirectory); Resource[] aSourceResources = sourceFactory.getResources(aSourceParams); Map<String, Object> aDestinationParams = new HashMap<String, Object>(); if (LOGGER.isInfoEnabled()) { LOGGER.info("{} file(s) to zip", aSourceResources.length); } if (aSourceResources.length > 0) { aDestinationParams.put(ResourceFactoryConstants.PARAM_BASE_DIRECTORY, sourceBaseDirectory); aDestinationParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC, ((sChunkContext != null) && (sChunkContext.getStepContext() != null)) ? sChunkContext.getStepContext().getStepExecution() : null); Resource aDestination = destinationFactory.getResource(aDestinationParams); ZipArchiveOutputStream aZipArchiveOutputStream = null; try { aZipArchiveOutputStream = new ZipArchiveOutputStream(aDestination.getFile()); sourceBaseDirectoryPath = sourceBaseDirectory.getFile().getCanonicalPath(); for (Resource aSourceResource : aSourceResources) { zipResource(aSourceResource, aZipArchiveOutputStream, sContribution, sChunkContext); } } finally { if (aZipArchiveOutputStream != null) { aZipArchiveOutputStream.finish(); aZipArchiveOutputStream.close(); } } } return RepeatStatus.FINISHED; }
From source file:com.amazon.aws.samplecode.travellog.util.DataExtractor.java
public void run() { try {// w ww . java 2 s.com //Create temporary directory File tmpDir = File.createTempFile("travellog", ""); tmpDir.delete(); //Wipe out temporary file to replace with a directory tmpDir.mkdirs(); logger.log(Level.INFO, "Extract temp dir: " + tmpDir); //Store journal to props file Journal journal = dao.getJournal(); Properties journalProps = buildProps(journal); File journalFile = new File(tmpDir, "journal"); journalProps.store(new FileOutputStream(journalFile), ""); //Iterate through entries and grab related photos List<Entry> entries = dao.getEntries(journal); int entryIndex = 1; int imageFileIndex = 1; for (Entry entry : entries) { Properties entryProps = buildProps(entry); File entryFile = new File(tmpDir, "entry." + (entryIndex++)); entryProps.store(new FileOutputStream(entryFile), ""); List<Photo> photos = dao.getPhotos(entry); int photoIndex = 1; for (Photo photo : photos) { Properties photoProps = buildProps(photo); InputStream photoData = S3PhotoUtil.loadOriginalPhoto(photo); String imageFileName = "imgdata." + (imageFileIndex++); File imageFile = new File(tmpDir, imageFileName); FileOutputStream outputStream = new FileOutputStream(imageFile); IOUtils.copy(photoData, outputStream); photoProps.setProperty("file", imageFileName); outputStream.close(); photoData.close(); File photoFile = new File(tmpDir, "photo." + (entryIndex - 1) + "." + (photoIndex++)); photoProps.store(new FileOutputStream(photoFile), ""); } List<Comment> comments = dao.getComments(entry); int commentIndex = 1; for (Comment comment : comments) { Properties commentProps = buildProps(comment); File commentFile = new File(tmpDir, "comment." + (entryIndex - 1) + "." + commentIndex++); commentProps.store(new FileOutputStream(commentFile), ""); } } //Bundle up the folder as a zip final File zipOut; //If we have an output path store locally if (outputPath != null) { zipOut = new File(outputPath); } else { //storing to S3 zipOut = File.createTempFile("export", ".zip"); } zipOut.getParentFile().mkdirs(); //make sure directory structure is in place ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(zipOut); //Create the zip file File[] files = tmpDir.listFiles(); for (File file : files) { ZipArchiveEntry archiveEntry = new ZipArchiveEntry(file.getName()); byte[] fileData = FileUtils.readFileToByteArray(file); archiveEntry.setSize(fileData.length); zaos.putArchiveEntry(archiveEntry); zaos.write(fileData); zaos.flush(); zaos.closeArchiveEntry(); } zaos.close(); //If outputpath if (outputPath == null) { TravelLogStorageObject obj = new TravelLogStorageObject(); obj.setBucketName(bucketName); obj.setStoragePath(storagePath); obj.setData(FileUtils.readFileToByteArray(zipOut)); obj.setMimeType("application/zip"); S3StorageManager mgr = new S3StorageManager(); mgr.store(obj, false, null); //Store with full redundancy and default permissions } } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } }