List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream close
public void close() throws IOException
From source file:fr.gael.ccsds.sip.archive.ZipArchiveManager.java
/** * Produces Zip compressed archive.//from w w w.j a v a 2 s . c o m */ @Override public File copy(final File src, final File zip_file, final String dst) throws Exception { if (zip_file.exists()) { final FileInputStream fis = new FileInputStream(zip_file); final ZipArchiveInputStream zis = new ZipArchiveInputStream(fis); final File tempFile = File.createTempFile("updateZip", "zip"); final FileOutputStream fos = new FileOutputStream(tempFile); final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); // copy the existing entries ZipArchiveEntry nextEntry; while ((nextEntry = zis.getNextZipEntry()) != null) { zos.putArchiveEntry(nextEntry); IOUtils.copy(zis, zos); zos.closeArchiveEntry(); } // create the new entry final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst); entry.setSize(src.length()); zos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, zos); sfis.close(); zos.closeArchiveEntry(); zos.finish(); zis.close(); fis.close(); zos.close(); // Rename the new file over the old boolean status = zip_file.delete(); File saved_tempFile = tempFile; status = tempFile.renameTo(zip_file); // Copy the new file over the old if the renaming failed if (!status) { final FileInputStream tfis = new FileInputStream(saved_tempFile); final FileOutputStream tfos = new FileOutputStream(zip_file); final byte[] buf = new byte[1024]; int i = 0; while ((i = tfis.read(buf)) != -1) { tfos.write(buf, 0, i); } tfis.close(); tfos.close(); saved_tempFile.delete(); } return zip_file; } else { final FileOutputStream fos = new FileOutputStream(zip_file); final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst); entry.setSize(src.length()); zos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, zos); sfis.close(); zos.closeArchiveEntry(); zos.finish(); zos.close(); fos.close(); } return zip_file; }
From source file:com.xpn.xwiki.plugin.packaging.AbstractPackageTest.java
/** * Create a XAR file using commons compress. * * @param docs The documents to include. * @param encodings The charset for each document. * @param packageXmlEncoding The encoding of package.xml * @return the XAR file as a byte array. *//* www . j av a 2 s .c o m*/ protected byte[] createZipFileUsingCommonsCompress(XWikiDocument docs[], String[] encodings, String packageXmlEncoding) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(baos); ZipArchiveEntry zipp = new ZipArchiveEntry("package.xml"); zos.putArchiveEntry(zipp); zos.write(getEncodedByteArray(getPackageXML(docs, packageXmlEncoding), packageXmlEncoding)); for (int i = 0; i < docs.length; i++) { String zipEntryName = docs[i].getSpace() + "/" + docs[i].getName(); if (docs[i].getTranslation() != 0) { zipEntryName += "." + docs[i].getLanguage(); } ZipArchiveEntry zipe = new ZipArchiveEntry(zipEntryName); zos.putArchiveEntry(zipe); String xmlCode = docs[i].toXML(false, false, false, false, getContext()); zos.write(getEncodedByteArray(xmlCode, encodings[i])); zos.closeArchiveEntry(); } zos.finish(); zos.close(); return baos.toByteArray(); }
From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidSourceTest.java
@Test public void test() { ZipArchiveOutputStream zos = null; File archive;//from w w w . j ava 2 s.c o m String filename = ""; try { archive = File.createTempFile("test.", ".zip", null); archive.deleteOnExit(); filename = archive.getAbsolutePath(); zos = new ZipArchiveOutputStream(archive); for (int z = 0; z < 3; z++) { for (int x = 0; x < Math.pow(2, z); x++) { for (int y = 0; y < Math.pow(2, z); y++) { ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/tiles/" + z + "/" + x + "/" + y + ".dummy"); zos.putArchiveEntry(entry); } } } ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json"); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); zos.close(); zos = null; } catch (IOException e) { fail(e.getMessage()); } try { ZipResourcePyramidSource src = new ZipResourcePyramidSource(filename, "dummy"); TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1); InputStream is = src.getSourceTileStream("test", tileDef); Assert.assertTrue(is != null); tileDef = new TileIndex(2, 3, 3, 1, 1); is = src.getSourceTileStream("test", tileDef); Assert.assertTrue(is != null); is = src.getSourceMetaDataStream("test"); Assert.assertTrue(is != null); } catch (IOException e) { fail(e.getMessage()); } }
From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidStreamSourceTest.java
@Test public void test() { ZipArchiveOutputStream zos = null; File archive;// w w w. j a va 2 s . c o m String filename = ""; try { archive = File.createTempFile("test.", ".zip", null); archive.deleteOnExit(); filename = archive.getAbsolutePath(); zos = new ZipArchiveOutputStream(archive); for (int z = 0; z < 3; z++) { for (int x = 0; x < Math.pow(2, z); x++) { for (int y = 0; y < Math.pow(2, z); y++) { ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/tiles/" + z + "/" + x + "/" + y + ".dummy"); zos.putArchiveEntry(entry); } } } ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json"); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); zos.close(); zos = null; } catch (IOException e) { fail(e.getMessage()); } try { ZipResourcePyramidStreamSource src = new ZipResourcePyramidStreamSource(filename, "dummy"); TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1); InputStream is = src.getTileStream("test", tileDef); Assert.assertTrue(is != null); tileDef = new TileIndex(2, 3, 3, 1, 1); is = src.getTileStream("test", tileDef); Assert.assertTrue(is != null); is = src.getMetaDataStream("test"); Assert.assertTrue(is != null); } catch (IOException e) { fail(e.getMessage()); } }
From source file:net.larry1123.elec.util.logger.LoggerDirectoryHandler.java
public void zipLogs() throws IOException { File zipFile = getDirectoryPath().resolve(getDateFormatFromMilli(System.currentTimeMillis()) + ".zip") .toFile();//ww w.j av a 2s .c om ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(zipFile); // Collection<File> files = FileUtils.listFiles(directory, new String[] {getConfig().getFileType()}, false); // for (File file : files) { // packFile(file, archiveOutputStream); // } for (Map.Entry<Logger, UtilFileHandler> loggerUtilFileHandlerEntry : streamHandlerHashMap.entrySet()) { Logger logger = loggerUtilFileHandlerEntry.getKey(); UtilFileHandler utilFileHandler = loggerUtilFileHandlerEntry.getValue(); // Stop the steam and make sure it is all to disk first utilFileHandler.close(); File file = getFileForLogger(logger); packFile(file, archiveOutputStream); // Restart the stream utilFileHandler.setOutputStream(makeFileOutputStreamForLogger(logger)); } archiveOutputStream.finish(); archiveOutputStream.close(); }
From source file:com.haulmont.cuba.core.app.FoldersServiceBean.java
@Override public byte[] exportFolder(Folder folder) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(StandardCharsets.UTF_8.name()); String xml = createXStream().toXML(folder); byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8); ArchiveEntry zipEntryDesign = newStoredEntry("folder.xml", xmlBytes); zipOutputStream.putArchiveEntry(zipEntryDesign); zipOutputStream.write(xmlBytes);//from w w w . j a v a2 s.c o m try { zipOutputStream.closeArchiveEntry(); } catch (Exception ex) { throw new RuntimeException( String.format("Exception occurred while exporting folder %s.", folder.getName())); } zipOutputStream.close(); return byteArrayOutputStream.toByteArray(); }
From source file:com.atolcd.web.scripts.ZipContents.java
public void createZipFile(List<String> nodeIds, OutputStream os, boolean noaccent) throws IOException { File zip = null;//w w w .j a v a2 s .c o m try { if (nodeIds != null && !nodeIds.isEmpty()) { zip = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ZIP_EXTENSION); FileOutputStream stream = new FileOutputStream(zip); CheckedOutputStream checksum = new CheckedOutputStream(stream, new Adler32()); BufferedOutputStream buff = new BufferedOutputStream(checksum); ZipArchiveOutputStream out = new ZipArchiveOutputStream(buff); out.setEncoding(encoding); out.setMethod(ZipArchiveOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); if (logger.isDebugEnabled()) { logger.debug("Using encoding '" + encoding + "' for zip file."); } try { for (String nodeId : nodeIds) { NodeRef node = new NodeRef(storeRef, nodeId); addToZip(node, out, noaccent, ""); } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { out.close(); buff.close(); checksum.close(); stream.close(); if (nodeIds.size() > 0) { InputStream in = new FileInputStream(zip); try { byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { os.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(in); } } } } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { // try and delete the temporary file if (zip != null) { zip.delete(); } } }
From source file:io.selendroid.builder.SelendroidServerBuilder.java
File createAndAddCustomizedAndroidManifestToSelendroidServer() throws IOException, ShellCommandException, AndroidSdkException { String targetPackageName = applicationUnderTest.getBasePackage(); File tempdir = new File(FileUtils.getTempDirectoryPath() + File.separatorChar + targetPackageName + System.currentTimeMillis()); if (!tempdir.exists()) { tempdir.mkdirs();/*from w ww . ja v a2s .c om*/ } File customizedManifest = new File(tempdir, "AndroidManifest.xml"); log.info("Adding target package '" + targetPackageName + "' to " + customizedManifest.getAbsolutePath()); // add target package InputStream inputStream = getResourceAsStream(selendroidApplicationXmlTemplate); if (inputStream == null) { throw new SelendroidException("AndroidApplication.xml template file was not found."); } String content = IOUtils.toString(inputStream, Charset.defaultCharset().displayName()); // find the first occurance of "package" and appending the targetpackagename to begining int i = content.toLowerCase().indexOf("package"); int cnt = 0; for (; i < content.length(); i++) { if (content.charAt(i) == '\"') { cnt++; } if (cnt == 2) { break; } } content = content.substring(0, i) + "." + targetPackageName + content.substring(i); log.info("Final Manifest File:\n" + content); content = content.replaceAll(SELENDROID_TEST_APP_PACKAGE, targetPackageName); // Seems like this needs to be done if (content.contains(ICON)) { content = content.replaceAll(ICON, ""); } OutputStream outputStream = new FileOutputStream(customizedManifest); IOUtils.write(content, outputStream, Charset.defaultCharset().displayName()); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); // adding the xml to an empty apk CommandLine createManifestApk = new CommandLine(AndroidSdk.aapt()); createManifestApk.addArgument("package", false); createManifestApk.addArgument("-M", false); createManifestApk.addArgument(customizedManifest.getAbsolutePath(), false); createManifestApk.addArgument("-I", false); createManifestApk.addArgument(AndroidSdk.androidJar(), false); createManifestApk.addArgument("-F", false); createManifestApk.addArgument(tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk", false); createManifestApk.addArgument("-f", false); log.info(ShellCommand.exec(createManifestApk, 20000L)); ZipFile manifestApk = new ZipFile( new File(tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk")); ZipArchiveEntry binaryManifestXml = manifestApk.getEntry("AndroidManifest.xml"); File finalSelendroidServerFile = new File(tempdir.getAbsolutePath() + "selendroid-server.apk"); ZipArchiveOutputStream finalSelendroidServer = new ZipArchiveOutputStream(finalSelendroidServerFile); finalSelendroidServer.putArchiveEntry(binaryManifestXml); IOUtils.copy(manifestApk.getInputStream(binaryManifestXml), finalSelendroidServer); ZipFile selendroidPrebuildApk = new ZipFile(selendroidServer.getAbsolutePath()); Enumeration<ZipArchiveEntry> entries = selendroidPrebuildApk.getEntries(); for (; entries.hasMoreElements();) { ZipArchiveEntry dd = entries.nextElement(); finalSelendroidServer.putArchiveEntry(dd); IOUtils.copy(selendroidPrebuildApk.getInputStream(dd), finalSelendroidServer); } finalSelendroidServer.closeArchiveEntry(); finalSelendroidServer.close(); manifestApk.close(); log.info("file: " + finalSelendroidServerFile.getAbsolutePath()); return finalSelendroidServerFile; }
From source file:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java
/** * Creates a zip file with random content. * * @author S3460/*from w w w . j av a 2s . c o m*/ * @param source the source * @return the zip file * @throws Exception the exception */ private ZipFile makeSourceZipFile(File source) throws Exception { ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(source)); int size = randomSize(entryMaxSize); for (int i = 0; i < size; i++) { out.putArchiveEntry(new ZipArchiveEntry("zipentry" + i)); int anz = randomSize(10); for (int j = 0; j < anz; j++) { byte[] bytes = getRandomBytes(); out.write(bytes, 0, bytes.length); } out.flush(); out.closeArchiveEntry(); } //add leeres Entry out.putArchiveEntry(new ZipArchiveEntry("zipentry" + size)); out.flush(); out.closeArchiveEntry(); out.flush(); out.finish(); out.close(); return new ZipFile(source); }
From source file:io.selendroid.standalone.builder.SelendroidServerBuilder.java
File createAndAddCustomizedAndroidManifestToSelendroidServer() throws IOException, ShellCommandException, AndroidSdkException { String targetPackageName = applicationUnderTest.getBasePackage(); File tmpDir = Files.createTempDir(); if (deleteTmpFiles()) { tmpDir.deleteOnExit();// ww w.j a va 2s . c o m } File customizedManifest = new File(tmpDir, "AndroidManifest.xml"); if (deleteTmpFiles()) { customizedManifest.deleteOnExit(); } log.info("Adding target package '" + targetPackageName + "' to " + customizedManifest.getAbsolutePath()); // add target package InputStream inputStream = getResourceAsStream(selendroidApplicationXmlTemplate); if (inputStream == null) { throw new SelendroidException("AndroidApplication.xml template file was not found."); } String content = IOUtils.toString(inputStream, Charset.defaultCharset().displayName()); // find the first occurance of "package" and appending the targetpackagename to begining int i = content.toLowerCase().indexOf("package"); int cnt = 0; for (; i < content.length(); i++) { if (content.charAt(i) == '\"') { cnt++; } if (cnt == 2) { break; } } content = content.substring(0, i) + "." + targetPackageName + content.substring(i); log.info("Final Manifest File:\n" + content); content = content.replaceAll(SELENDROID_TEST_APP_PACKAGE, targetPackageName); // Seems like this needs to be done if (content.contains(ICON)) { content = content.replaceAll(ICON, ""); } OutputStream outputStream = new FileOutputStream(customizedManifest); IOUtils.write(content, outputStream, Charset.defaultCharset().displayName()); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); // adding the xml to an empty apk CommandLine createManifestApk = new CommandLine(AndroidSdk.aapt()); File manifestApkFile = File.createTempFile("manifest", ".apk"); if (deleteTmpFiles()) { manifestApkFile.deleteOnExit(); } createManifestApk.addArgument("package", false); createManifestApk.addArgument("-M", false); createManifestApk.addArgument(customizedManifest.getAbsolutePath(), false); createManifestApk.addArgument("-I", false); createManifestApk.addArgument(AndroidSdk.androidJar(), false); createManifestApk.addArgument("-F", false); createManifestApk.addArgument(manifestApkFile.getAbsolutePath(), false); createManifestApk.addArgument("-f", false); log.info(ShellCommand.exec(createManifestApk, 20000L)); ZipFile manifestApk = new ZipFile(manifestApkFile); ZipArchiveEntry binaryManifestXml = manifestApk.getEntry("AndroidManifest.xml"); File finalSelendroidServerFile = File.createTempFile("selendroid-server", ".apk"); if (deleteTmpFiles()) { finalSelendroidServerFile.deleteOnExit(); } ZipArchiveOutputStream finalSelendroidServer = new ZipArchiveOutputStream(finalSelendroidServerFile); finalSelendroidServer.putArchiveEntry(binaryManifestXml); IOUtils.copy(manifestApk.getInputStream(binaryManifestXml), finalSelendroidServer); ZipFile selendroidPrebuildApk = new ZipFile(selendroidServer.getAbsolutePath()); Enumeration<ZipArchiveEntry> entries = selendroidPrebuildApk.getEntries(); for (; entries.hasMoreElements();) { ZipArchiveEntry dd = entries.nextElement(); finalSelendroidServer.putArchiveEntry(dd); IOUtils.copy(selendroidPrebuildApk.getInputStream(dd), finalSelendroidServer); } finalSelendroidServer.closeArchiveEntry(); finalSelendroidServer.close(); manifestApk.close(); log.info("file: " + finalSelendroidServerFile.getAbsolutePath()); return finalSelendroidServerFile; }