List of usage examples for java.util.zip ZipOutputStream ZipOutputStream
public ZipOutputStream(OutputStream out)
From source file:com.chinamobile.bcbsp.fault.tools.Zip.java
/** * Compress files to *.zip.//from w w w .j a v a 2 s.co m * @param fileName * file name to compress * @return compressed file. */ public static String compress(String fileName) { String targetFile = null; File sourceFile = new File(fileName); Vector<File> vector = getAllFiles(sourceFile); try { if (sourceFile.isDirectory()) { targetFile = fileName + ".zip"; } else { char ch = '.'; targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip"; } BufferedInputStream bis = null; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)); ZipOutputStream zipos = new ZipOutputStream(bos); byte[] data = new byte[BUFFER]; if (vector.size() > 0) { for (int i = 0; i < vector.size(); i++) { File file = vector.get(i); zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file))); bis = new BufferedInputStream(new FileInputStream(file)); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipos.write(data, 0, count); } bis.close(); zipos.closeEntry(); } zipos.close(); bos.close(); return targetFile; } else { File zipNullfile = new File(targetFile); zipNullfile.getParentFile().mkdirs(); zipNullfile.mkdir(); return zipNullfile.getAbsolutePath(); } } catch (IOException e) { LOG.error("[compress]", e); return "error"; } }
From source file:com.facebook.buck.zip.ZipScrubberStepIntegrationTest.java
@Test public void modificationTimes() throws Exception { // Create a dummy ZIP file. Path zip = tmp.newFile("output.zip"); try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zip))) { ZipEntry entry = new ZipEntry("file1"); byte[] data = "data1".getBytes(Charsets.UTF_8); entry.setSize(data.length);//from ww w.j a va 2 s . co m out.putNextEntry(entry); out.write(data); out.closeEntry(); entry = new ZipEntry("file2"); data = "data2".getBytes(Charsets.UTF_8); entry.setSize(data.length); out.putNextEntry(entry); out.write(data); out.closeEntry(); } // Execute the zip scrubber step. ExecutionContext executionContext = TestExecutionContext.newInstance(); ZipScrubberStep step = new ZipScrubberStep(new ProjectFilesystem(tmp.getRoot()), Paths.get("output.zip")); assertEquals(0, step.execute(executionContext).getExitCode()); // Iterate over each of the entries, expecting to see all zeros in the time fields. Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME)); try (ZipInputStream is = new ZipInputStream(new FileInputStream(zip.toFile()))) { for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) { assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch)); } } }
From source file:com.stehno.oxy.ZipBuilder.java
/** * Creates a zip builder with the given output stream. The stream will be closed when the <code>zip()</code> * method is called./*from w ww . j ava 2 s . c o m*/ * * @param os the output stream the zip file is to be written into */ public ZipBuilder(final OutputStream os) { super(); this.zos = new ZipOutputStream(os); }
From source file:org.cloudfoundry.client.lib.io.DynamicZipInputStreamTest.java
@Test public void shouldCreateValidZipContent() throws Exception { byte[] f1 = newRandomBytes(10000); byte[] f2 = newRandomBytes(10000); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(bos); zipOutputStream.putNextEntry(new ZipEntry("a/b/c")); zipOutputStream.write(f1);// w w w .j av a2 s.c o m zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("a/b/c/d/")); zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("d/e/f")); zipOutputStream.write(f2); zipOutputStream.closeEntry(); zipOutputStream.flush(); zipOutputStream.close(); byte[] expected = bos.toByteArray(); List<DynamicZipInputStream.Entry> entries = new ArrayList<DynamicZipInputStream.Entry>(); entries.add(newEntry("a/b/c", f1)); entries.add(newEntry("a/b/c/d/", null)); entries.add(newEntry("d/e/f", f2)); DynamicZipInputStream inputStream = new DynamicZipInputStream(entries); bos.reset(); FileCopyUtils.copy(inputStream, bos); byte[] actual = bos.toByteArray(); assertThat(actual, is(equalTo(expected))); }
From source file:eu.scidipes.toolkits.palibrary.utils.zip.ZipUtils.java
/** * Zips one or more {@link ByteArrayZipEntry} objects together and returns the archive as a base64-encoded * <code>String</code>//from w w w . j av a 2s . co m * * @param entries * @return a base64-encoded <code>String</code> which is the zip archive of the passed entries * * @throws IOException * in the event of an exception writing to the zip output stream * @throws IllegalArgumentException * if passed entries is null or empty */ public static String byteArrayZipEntriesToBase64(final Set<? extends ByteArrayZipEntry> entries) throws IOException { if (entries == null || entries.isEmpty()) { throw new IllegalArgumentException("entries cannot be null or empty"); } final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (final ZipOutputStream zos = new ZipOutputStream(bos)) { for (final ByteArrayZipEntry entry : entries) { zos.putNextEntry(entry.getZipEntry()); IOUtils.write(entry.getBytes(), zos); zos.closeEntry(); } } return Base64.encodeBase64String(bos.toByteArray()); }
From source file:net.sourceforge.floggy.persistence.pool.ZipOutputPool.java
/** * Creates a new ZipOutputPool object.//from w ww. j a va 2 s. co m * * @param file DOCUMENT ME! * * @throws IOException DOCUMENT ME! * @throws IllegalArgumentException DOCUMENT ME! */ public ZipOutputPool(File file) throws IOException { if (file == null) { throw new IllegalArgumentException("The file parameter cannot be null!"); } out = new ZipOutputStream(new FileOutputStream(file)); }
From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java
public Message toMessage(Object object, MessageProperties props) throws MessageConversionException { if (object instanceof BatchMessage) { BatchMessage batch = (BatchMessage) object; props.setCorrelationId(batch.getId().getBytes()); props.setContentType("application/zip"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ZipOutputStream zout = new ZipOutputStream(bout); for (Map.Entry<String, String> msg : batch.getMessages().entrySet()) { ZipEntry zentry = new ZipEntry(msg.getKey()); try { zout.putNextEntry(zentry); zout.write(msg.getValue().getBytes()); zout.closeEntry();// ww w . j a v a 2 s. co m } catch (IOException e) { throw new MessageConversionException(e.getMessage(), e); } } try { zout.flush(); zout.close(); } catch (IOException e) { throw new MessageConversionException(e.getMessage(), e); } return new Message(bout.toByteArray(), props); } else { throw new MessageConversionException( "Cannot convert object " + String.valueOf(object) + " using " + getClass().toString()); } }
From source file:mpimp.assemblxweb.util.J5FileUtils.java
public static void zipDirectory(String dirPath, String targetPath) throws Exception { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(targetPath); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); out.setMethod(ZipOutputStream.DEFLATED); int BUFFER = 2048; byte data[] = new byte[BUFFER]; zipDirectoryRecursively(dirPath, origin, BUFFER, out, data, new File(dirPath).getName()); out.close();/*from ww w . j a va 2 s.c o m*/ }
From source file:com.baasbox.db.async.ExportJob.java
@Override public void run() { FileOutputStream dest = null; ZipOutputStream zip = null;//from w ww . ja va2 s. co m FileOutputStream tempJsonOS = null; FileInputStream in = null; try { //File f = new File(this.fileName); File f = File.createTempFile("export", ".temp"); dest = new FileOutputStream(f); zip = new ZipOutputStream(dest); File tmpJson = File.createTempFile("export", ".json"); tempJsonOS = new FileOutputStream(tmpJson); DbHelper.exportData(this.appcode, tempJsonOS); BaasBoxLogger.info(String.format("Writing %d bytes ", tmpJson.length())); tempJsonOS.close(); ZipEntry entry = new ZipEntry("export.json"); zip.putNextEntry(entry); in = new FileInputStream(tmpJson); final int BUFFER = BBConfiguration.getImportExportBufferSize(); byte buffer[] = new byte[BUFFER]; int length; while ((length = in.read(buffer)) > 0) { zip.write(buffer, 0, length); } zip.closeEntry(); in.close(); File manifest = File.createTempFile("manifest", ".txt"); FileUtils.writeStringToFile(manifest, BBInternalConstants.IMPORT_MANIFEST_VERSION_PREFIX + BBConfiguration.getApiVersion()); ZipEntry entryManifest = new ZipEntry("manifest.txt"); zip.putNextEntry(entryManifest); zip.write(FileUtils.readFileToByteArray(manifest)); zip.closeEntry(); tmpJson.delete(); manifest.delete(); File finaldestination = new File(this.fileName); FileUtils.moveFile(f, finaldestination); } catch (Exception e) { BaasBoxLogger.error(ExceptionUtils.getMessage(e)); } finally { try { if (zip != null) zip.close(); if (dest != null) dest.close(); if (tempJsonOS != null) tempJsonOS.close(); if (in != null) in.close(); } catch (Exception ioe) { BaasBoxLogger.error(ExceptionUtils.getMessage(ioe)); } } }
From source file:net.sf.jabref.exporter.OpenOfficeDocumentCreator.java
private static void storeOpenOfficeFile(File file, InputStream source) throws Exception { try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { ZipEntry zipEntry = new ZipEntry("content.xml"); out.putNextEntry(zipEntry);//from w w w . ja v a 2 s .c o m int c; while ((c = source.read()) >= 0) { out.write(c); } out.closeEntry(); // Add manifest (required for OOo 2.0), "meta.xml", "mimetype" files. These are in the // resource/openoffice directory, and are copied verbatim into the zip file. OpenOfficeDocumentCreator.addResourceFile("meta.xml", "/resource/openoffice/meta.xml", out); OpenOfficeDocumentCreator.addResourceFile("mimetype", "/resource/openoffice/mimetype", out); OpenOfficeDocumentCreator.addResourceFile("META-INF/manifest.xml", "/resource/openoffice/manifest.xml", out); } }