List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
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);//from w w w . j av a 2 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:de.thischwa.pmcms.tool.compression.Zip.java
/** * Static method to compress all files based on the ImputStream in 'entries' into 'zip'. * Each entry has a InputStream and its String representation in the zip. * //from www .j a va2 s.c o m * @param zip The zip file. It will be deleted if exists. * @param entries Map<File, String> * @param monitor Must be initialized correctly by the caller. * @throws IOException */ public static void compress(final File zip, final Map<InputStream, String> entries, final IProgressMonitor monitor) throws IOException { if (zip == null || entries == null || CollectionUtils.isEmpty(entries.keySet())) throw new IllegalArgumentException("One ore more parameters are empty!"); if (zip.exists()) zip.delete(); else if (!zip.getParentFile().exists()) zip.getParentFile().mkdirs(); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip))); out.setLevel(Deflater.BEST_COMPRESSION); try { for (InputStream inputStream : entries.keySet()) { // skip beginning slash, because can cause errors in other zip apps ZipEntry zipEntry = new ZipEntry(skipBeginningSlash(entries.get(inputStream))); out.putNextEntry(zipEntry); IOUtils.copy(inputStream, out); out.closeEntry(); inputStream.close(); if (monitor != null) monitor.worked(1); } } finally { // cleanup IOUtils.closeQuietly(out); } }
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 ava 2s. 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:de.berlios.jedi.logic.editor.JispPackageJispFileStorer.java
/** * Adds a new file to the ZipOutputStream, given the filename and its data. * /*from ww w . ja v a 2 s.com*/ * @param filename * The name of the file to add. * @param data * The data of the file. */ protected void addFileToStore(String filename, byte[] data) { ZipEntry zipEntry = new ZipEntry(filename); try { outZipped.putNextEntry(zipEntry); outZipped.write(data); outZipped.closeEntry(); } catch (IOException e) { LogFactory.getLog(JispPackageJispFileStorer.class) .warn("A file couldn't be added to the ZipOutputStream", e); } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param basePath/*w w w . j a v a2 s.c om*/ * @param zipPath * @param filePaths * @throws java.io.IOException */ public static void zip(File basePath, File zipPath, Map<String, String> filePaths) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = null; FileOutputStream dest = null; try { dest = new FileOutputStream(zipPath); out = new ZipOutputStream(new BufferedOutputStream(dest)); //out.setMethod(ZipOutputStream.DEFLATED); byte data[] = new byte[BUFFER]; for (Map.Entry<String, String> file : filePaths.entrySet()) { String filename = file.getKey(); String filePath = file.getValue(); System.out.println("Adding: " + basePath.getPath() + filePath + " => " + filename); File f = new File(basePath, filePath); if (f.exists()) { FileInputStream fi = new FileInputStream(f); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(filename); entry.setCrc(FileUtils.checksumCRC32(new File(basePath, filePath))); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } } } finally { if (origin != null) origin.close(); if (out != null) out.close(); if (dest != null) dest.close(); } }
From source file:Main.java
static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException { // if we reached here, the File object f was not a directory // create a FileInputStream on top of f FileInputStream fis = new FileInputStream(zipfile); try {//from w ww . j a v a2 s.c o m // create a new zip entry ZipEntry anEntry = new ZipEntry(name); if (DEBUG) System.out.println("Add file : " + name); // place the zip entry in the ZipOutputStream object zos.putNextEntry(anEntry); // now write the content of the file to the // ZipOutputStream byte[] readBuffer = new byte[BUFFER_SIZE]; for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis.read(readBuffer)) { zos.write(readBuffer, 0, bytesIn); } } finally { // close the Stream fis.close(); } }
From source file:com.plotsquared.iserver.util.FileUtils.java
/** * Add files to a zip file/* w ww . j a v a2s . c om*/ * * @param zipFile Zip File * @param files Files to add to the zip * @param delete If the original files should be deleted * @throws Exception If anything goes wrong */ public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception { Assert.notNull(zipFile, files); if (!zipFile.exists()) { if (!zipFile.createNewFile()) { throw new RuntimeException("Couldn't create " + zipFile); } } final File temporary = File.createTempFile(zipFile.getName(), ""); //noinspection ResultOfMethodCallIgnored temporary.delete(); if (!zipFile.renameTo(temporary)) { throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary); } final byte[] buffer = new byte[1024 * 16]; // 16mb ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry e = zis.getNextEntry(); while (e != null) { String n = e.getName(); boolean no = true; for (File f : files) { if (f.getName().equals(n)) { no = false; break; } } if (no) { zos.putNextEntry(new ZipEntry(n)); int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } e = zis.getNextEntry(); } zis.close(); for (File file : files) { InputStream in = new FileInputStream(file); zos.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); in.close(); } zos.close(); temporary.delete(); if (delete) { for (File f : files) { f.delete(); } } }
From source file:de.xwic.appkit.core.util.ZipUtil.java
/** * Zips the files array into a file that has the name given as parameter. * // w ww .ja v a2s. com * @param files * the files array * @param zipFileName * the name for the zip file * @return the new zipped file * @throws IOException */ public static File zip(File[] files, String zipFileName) throws IOException { FileOutputStream stream = null; ZipOutputStream out = null; File archiveFile = null; try { if (!zipFileName.endsWith(".zip")) { zipFileName = zipFileName + ".zip"; } archiveFile = new File(zipFileName); byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file stream = new FileOutputStream(archiveFile); out = new ZipOutputStream(stream); for (int i = 0; i < files.length; i++) { if (null == files[i] || !files[i].exists() || files[i].isDirectory()) { continue; } log.info("Zipping " + files[i].getName()); // Add archive entry ZipEntry zipAdd = new ZipEntry(files[i].getName()); zipAdd.setTime(files[i].lastModified()); out.putNextEntry(zipAdd); // Read input & write to output FileInputStream in = new FileInputStream(files[i]); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) { break; } out.write(buffer, 0, nRead); } in.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } finally { try { if (null != out) { out.close(); } if (null != stream) { stream.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } } return archiveFile; }
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);/* w w w . ja v a2 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); } }
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();//from w w w .j a v a 2 s. c o 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()); } }