List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
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);// w w w .ja va 2 s . com 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.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. *//*from w w w .j a v a 2s. co 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:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java
/** * Creates a zip file with random content. * * @author S3460/*from ww w .ja va2 s. com*/ * @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:com.oculusinfo.binning.io.impl.ZipResourcePyramidSourceTest.java
@Test public void test() { ZipArchiveOutputStream zos = null; File archive;/*from w ww . j ava 2 s.co 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;/*from w w w .java2s .c om*/ 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: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();/* w ww. j a v a 2 s.c o m*/ } 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
/** * Writes a modified version of zip_Source into target. * * @author S3460//from w ww .j a va 2s . c o m * @param zipSource the zip source * @param target the target * @return the zip file * @throws Exception the exception */ private ZipFile makeTargetZipFile(ZipFile zipSource, File target) throws Exception { ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(target)); for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) { ZipArchiveEntry sourceEntry = enumer.nextElement(); out.putArchiveEntry(new ZipArchiveEntry(sourceEntry.getName())); byte[] oldBytes = toBytes(zipSource, sourceEntry); byte[] newBytes = getRandomBytes(); byte[] mixedBytes = mixBytes(oldBytes, newBytes); out.write(mixedBytes, 0, mixedBytes.length); out.flush(); out.closeArchiveEntry(); } out.putArchiveEntry(new ZipArchiveEntry("zipentry" + entryMaxSize + 1)); byte[] bytes = getRandomBytes(); out.write(bytes, 0, bytes.length); out.flush(); out.closeArchiveEntry(); out.putArchiveEntry(new ZipArchiveEntry("zipentry" + (entryMaxSize + 2))); out.closeArchiveEntry(); out.flush(); out.finish(); out.close(); return new ZipFile(targetFile); }
From source file:adams.core.io.ZipUtils.java
/** * Creates a zip file from the specified files. * * @param output the output file to generate * @param files the files to store in the zip file * @param stripRegExp the regular expression used to strip the file names (only applied to the directory!) * @param bufferSize the buffer size to use * @return null if successful, otherwise error message *///from w w w . ja va 2s. co m @MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/examples.html") public static String compress(File output, File[] files, String stripRegExp, int bufferSize) { String result; int i; byte[] buf; int len; ZipArchiveOutputStream out; BufferedInputStream in; FileInputStream fis; FileOutputStream fos; String filename; String msg; ZipArchiveEntry 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 ZIP file buf = new byte[bufferSize]; fos = new FileOutputStream(output.getAbsolutePath()); out = new ZipArchiveOutputStream(new BufferedOutputStream(fos)); for (i = 0; i < files.length; i++) { fis = new FileInputStream(files[i].getAbsolutePath()); in = new BufferedInputStream(fis); // Add ZIP 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 ZipArchiveEntry(filename); entry.setSize(files[i].length()); out.putArchiveEntry(entry); // Transfer bytes from the file to the ZIP 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 ZIP 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:com.haulmont.cuba.core.app.importexport.EntityImportExport.java
@Override public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) { String json = entitySerialization.toJson(entities, null, EntitySerializationOption.COMPACT_REPEATED_ENTITIES); byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(StandardCharsets.UTF_8.name()); ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes); try {/*w w w.j a v a 2 s. co m*/ zipOutputStream.putArchiveEntry(singleDesignEntry); zipOutputStream.write(jsonBytes); zipOutputStream.closeArchiveEntry(); } catch (Exception e) { throw new RuntimeException("Error on creating zip archive during entities export", e); } finally { IOUtils.closeQuietly(zipOutputStream); } return byteArrayOutputStream.toByteArray(); }
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();//from www.j a v a 2s.com } 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; }