List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:ZipTransformTest.java
public void testStreamTransformerIdentity() throws IOException { final String name = "foo"; final byte[] contents = "bar".getBytes(); File file1 = File.createTempFile("temp", null); File file2 = File.createTempFile("temp", null); try {//from w w w. ja v a2s . c om // Create the ZIP file ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1)); try { zos.putNextEntry(new ZipEntry(name)); zos.write(contents); zos.closeEntry(); } finally { IOUtils.closeQuietly(zos); } // Transform the ZIP file ZipUtil.transformEntry(file1, name, new StreamZipEntryTransformer() { protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException { IOUtils.copy(in, out); } }, file2); // Test the ZipUtil byte[] actual = ZipUtil.unpackEntry(file2, name); assertNotNull(actual); assertEquals(new String(contents), new String(actual)); } finally { FileUtils.deleteQuietly(file1); FileUtils.deleteQuietly(file2); } }
From source file:ZipHandler.java
/** * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL> * @param zipURL/*w ww .j ava2 s. c om*/ * @param xmlURL * @param xmlFileName */ public static void makeStructZip(String zipURL, String xmlURL, String xmlFileName) throws IOException { FileOutputStream fos = new FileOutputStream(new File(zipURL)); ZipOutputStream zos = new ZipOutputStream(fos); // zos.setLevel(); FileInputStream fis = new FileInputStream(xmlURL); zos.putNextEntry(new ZipEntry(xmlFileName + ".xml")); writeInOutputStream(fis, zos); zos.closeEntry(); fis.close(); zos.close(); }
From source file:com.pieframework.runtime.utils.Zipper.java
public static void zip(String zipFile, Map<String, File> flist) { byte[] buf = new byte[1024]; try {//from w w w.jav a2s. co m // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); // Compress the files for (String url : flist.keySet()) { FileInputStream in = new FileInputStream(flist.get(url).getPath()); // Add ZIP entry to output stream. Zip entry should be relative out.putNextEntry(new ZipEntry(url)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (Exception e) { throw new RuntimeException("Encountered errors zipping file " + zipFile, e); } }
From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java
/** * Adds the given file to the given zip output stream using the given name as the zip entry name. This method will * NOT call finish on the zip output stream at the end. * //from w w w .jav a 2 s . c om * @param zos the zip output stream to add the file to. This stream must already be open. * @param file the file to put in the zip. * @param name the name to use for this zip entry. * @param addAsStored if true, then the file will be added to the zip as a STORED entry (e.g. without applying * compression to it); if false, then the file will be added to the zip as a DEFLATED entry. * @throws IOException if there is an error writing to the stream */ public void writeZipEntry(ZipOutputStream zos, File file, String name, boolean addAsStored) throws IOException { final ZipEntry ze = new ZipEntry(name); ze.setMethod(addAsStored ? ZipEntry.STORED : ZipEntry.DEFLATED); if (addAsStored) { ze.setSize(file.length()); ze.setCrc(FileUtils.checksumCRC32(file)); } zos.putNextEntry(ze); final InputStream is = FileUtils.openInputStream(file); IOUtils.copy(is, zos); zos.closeEntry(); zos.flush(); IOUtils.closeQuietly(is); }
From source file:msearch.io.MSFilmlisteSchreiben.java
public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) { MSLog.systemMeldung("Filme Schreiben (" + listeFilme.size() + " Filme) :"); File file = new File(datei); File dir = new File(file.getParent()); if (!dir.exists()) { if (!dir.mkdirs()) { MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG, "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart", "Kann den Pfad nicht anlegen: " + dir.toString()); }//from w w w .j a va2 s . co m } MSLog.systemMeldung(" --> Start Schreiben nach: " + datei); try { String sender = "", thema = ""; JsonFactory jsonF = new JsonFactory(); JsonGenerator jg; if (datei.endsWith(MSConst.FORMAT_XZ)) { LZMA2Options options = new LZMA2Options(); XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options); jg = jsonF.createGenerator(out); } else if (datei.endsWith(MSConst.FORMAT_BZ2)) { bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file), 9 /*Blocksize: 1 - 9*/); jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8); } else if (datei.endsWith(MSConst.FORMAT_ZIP)) { zipOutputStream = new ZipOutputStream(new FileOutputStream(file)); ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME); zipOutputStream.putNextEntry(entry); jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8); } else { jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8); } jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier jg.writeStartObject(); // Infos zur Filmliste jg.writeArrayFieldStart(ListeFilme.FILMLISTE); for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) { jg.writeString(listeFilme.metaDaten[i]); } jg.writeEndArray(); // Infos der Felder in der Filmliste jg.writeArrayFieldStart(ListeFilme.FILMLISTE); for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) { jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]); } jg.writeEndArray(); //Filme schreiben ListIterator<DatenFilm> iterator; DatenFilm datenFilm; iterator = listeFilme.listIterator(); while (iterator.hasNext()) { datenFilm = iterator.next(); jg.writeArrayFieldStart(DatenFilm.FILME_); for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) { int m = DatenFilm.COLUMN_NAMES_JSON[i]; if (m == DatenFilm.FILM_SENDER_NR) { if (datenFilm.arr[m].equals(sender)) { jg.writeString(""); } else { sender = datenFilm.arr[m]; jg.writeString(datenFilm.arr[m]); } } else if (m == DatenFilm.FILM_THEMA_NR) { if (datenFilm.arr[m].equals(thema)) { jg.writeString(""); } else { thema = datenFilm.arr[m]; jg.writeString(datenFilm.arr[m]); } } else { jg.writeString(datenFilm.arr[m]); } } jg.writeEndArray(); } jg.writeEndObject(); jg.close(); MSLog.systemMeldung(" --> geschrieben!"); } catch (Exception ex) { MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex, "nach: " + datei); } }
From source file:msearch.filmlisten.MSFilmlisteSchreiben.java
public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) { MSLog.systemMeldung("Filme schreiben (" + listeFilme.size() + " Filme) :"); File file = new File(datei); File dir = new File(file.getParent()); if (!dir.exists()) { if (!dir.mkdirs()) { MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG, "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart", "Kann den Pfad nicht anlegen: " + dir.toString()); }//from ww w .j av a 2s. c o m } MSLog.systemMeldung(" --> Start Schreiben nach: " + datei); try { String sender = "", thema = ""; JsonFactory jsonF = new JsonFactory(); JsonGenerator jg; if (datei.endsWith(MSConst.FORMAT_XZ)) { LZMA2Options options = new LZMA2Options(); XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options); jg = jsonF.createGenerator(out); } else if (datei.endsWith(MSConst.FORMAT_BZ2)) { bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file), 9 /*Blocksize: 1 - 9*/); jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8); } else if (datei.endsWith(MSConst.FORMAT_ZIP)) { zipOutputStream = new ZipOutputStream(new FileOutputStream(file)); ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME); zipOutputStream.putNextEntry(entry); jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8); } else { jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8); } jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier jg.writeStartObject(); // Infos zur Filmliste jg.writeArrayFieldStart(ListeFilme.FILMLISTE); for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) { jg.writeString(listeFilme.metaDaten[i]); } jg.writeEndArray(); // Infos der Felder in der Filmliste jg.writeArrayFieldStart(ListeFilme.FILMLISTE); for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) { jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]); } jg.writeEndArray(); //Filme schreiben ListIterator<DatenFilm> iterator; DatenFilm datenFilm; iterator = listeFilme.listIterator(); while (iterator.hasNext()) { datenFilm = iterator.next(); jg.writeArrayFieldStart(DatenFilm.FILME_); for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) { int m = DatenFilm.COLUMN_NAMES_JSON[i]; if (m == DatenFilm.FILM_SENDER_NR) { if (datenFilm.arr[m].equals(sender)) { jg.writeString(""); } else { sender = datenFilm.arr[m]; jg.writeString(datenFilm.arr[m]); } } else if (m == DatenFilm.FILM_THEMA_NR) { if (datenFilm.arr[m].equals(thema)) { jg.writeString(""); } else { thema = datenFilm.arr[m]; jg.writeString(datenFilm.arr[m]); } } else { jg.writeString(datenFilm.arr[m]); } } jg.writeEndArray(); } jg.writeEndObject(); jg.close(); MSLog.systemMeldung(" --> geschrieben!"); } catch (Exception ex) { MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex, "nach: " + datei); } }
From source file:com.metamx.druid.loading.S3SegmentPusher.java
@Override public DataSegment push(File file, DataSegment segment) throws IOException { log.info("Uploading [%s] to S3", file); String outputKey = JOINER.join(config.getBaseKey().isEmpty() ? null : config.getBaseKey(), segment.getDataSource(),// w w w .j a v a2 s .c o m String.format("%s_%s", segment.getInterval().getStart(), segment.getInterval().getEnd()), segment.getVersion(), segment.getShardSpec().getPartitionNum()); File indexFilesDir = file; long indexSize = 0; final File zipOutFile = File.createTempFile("druid", "index.zip"); ZipOutputStream zipOut = null; try { zipOut = new ZipOutputStream(new FileOutputStream(zipOutFile)); File[] indexFiles = indexFilesDir.listFiles(); for (File indexFile : indexFiles) { log.info("Adding indexFile[%s] with size[%,d]. Total size[%,d]", indexFile, indexFile.length(), indexSize); if (indexFile.length() >= Integer.MAX_VALUE) { throw new ISE("indexFile[%s] too large [%,d]", indexFile, indexFile.length()); } zipOut.putNextEntry(new ZipEntry(indexFile.getName())); IOUtils.copy(new FileInputStream(indexFile), zipOut); indexSize += indexFile.length(); } } finally { Closeables.closeQuietly(zipOut); } try { S3Object toPush = new S3Object(zipOutFile); final String outputBucket = config.getBucket(); toPush.setBucketName(outputBucket); toPush.setKey(outputKey + "/index.zip"); log.info("Pushing %s.", toPush); s3Client.putObject(outputBucket, toPush); DataSegment outputSegment = segment.withSize(indexSize).withLoadSpec(ImmutableMap .<String, Object>of("type", "s3_zip", "bucket", outputBucket, "key", toPush.getKey())); File descriptorFile = File.createTempFile("druid", "descriptor.json"); StreamUtils.copyToFileAndClose(new ByteArrayInputStream(jsonMapper.writeValueAsBytes(segment)), descriptorFile); S3Object descriptorObject = new S3Object(descriptorFile); descriptorObject.setBucketName(outputBucket); descriptorObject.setKey(outputKey + "/descriptor.json"); log.info("Pushing %s", descriptorObject); s3Client.putObject(outputBucket, descriptorObject); log.info("Deleting Index File[%s]", indexFilesDir); FileUtils.deleteDirectory(indexFilesDir); log.info("Deleting zipped index File[%s]", zipOutFile); zipOutFile.delete(); log.info("Deleting descriptor file[%s]", descriptorFile); descriptorFile.delete(); return outputSegment; } catch (NoSuchAlgorithmException e) { throw new IOException(e); } catch (S3ServiceException e) { throw new IOException(e); } }
From source file:herddb.upgrade.ZIPUtils.java
private static void addFileToZip(int skipprefix, File file, ZipOutputStream zipper) throws IOException { String raw = file.getAbsolutePath().replace("\\", "/"); if (raw.length() == skipprefix) { if (file.isDirectory()) { File[] listFiles = file.listFiles(); if (listFiles != null) { for (File child : listFiles) { addFileToZip(skipprefix, child, zipper); }/*from ww w. j av a 2 s. c o m*/ } } } else { String path = raw.substring(skipprefix + 1); if (file.isDirectory()) { ZipEntry entry = new ZipEntry(path); zipper.putNextEntry(entry); zipper.closeEntry(); File[] listFiles = file.listFiles(); if (listFiles != null) { for (File child : listFiles) { addFileToZip(skipprefix, child, zipper); } } } else { ZipEntry entry = new ZipEntry(path); zipper.putNextEntry(entry); try (FileInputStream in = new FileInputStream(file)) { IOUtils.copyLarge(in, zipper); } zipper.closeEntry(); } } }
From source file:com.liferay.ide.server.remote.AbstractRemoteServerPublisher.java
protected void addToZip(IPath path, IResource resource, ZipOutputStream zip, boolean adjustGMTOffset) throws IOException, CoreException { switch (resource.getType()) { case IResource.FILE: ZipEntry zipEntry = new ZipEntry(path.toString()); zip.putNextEntry(zipEntry);//from ww w . ja v a2 s . c o m InputStream contents = ((IFile) resource).getContents(); if (adjustGMTOffset) { TimeZone currentTimeZone = TimeZone.getDefault(); Calendar currentDt = new GregorianCalendar(currentTimeZone, Locale.getDefault()); // Get the Offset from GMT taking current TZ into account int gmtOffset = currentTimeZone.getOffset(currentDt.get(Calendar.ERA), currentDt.get(Calendar.YEAR), currentDt.get(Calendar.MONTH), currentDt.get(Calendar.DAY_OF_MONTH), currentDt.get(Calendar.DAY_OF_WEEK), currentDt.get(Calendar.MILLISECOND)); zipEntry.setTime(System.currentTimeMillis() + (gmtOffset * -1)); } try { IOUtils.copy(contents, zip); } finally { contents.close(); } break; case IResource.FOLDER: case IResource.PROJECT: IContainer container = (IContainer) resource; IResource[] members = container.members(); for (IResource res : members) { addToZip(path.append(res.getName()), res, zip, adjustGMTOffset); } } }
From source file:com.centurylink.mdw.common.service.JsonExport.java
/** * Default behavior adds zip entries for each property on the first (non-mdw) top-level object. *//*from ww w . ja v a 2s . c o m*/ public String exportZipBase64() throws JSONException, IOException { Map<String, JSONObject> objectMap = JsonUtil.getJsonObjects(jsonable.getJson()); JSONObject mdw = null; JSONObject contents = null; for (String name : objectMap.keySet()) { if ("mdw".equals(name)) mdw = objectMap.get(name); else if (contents == null) contents = objectMap.get(name); } if (contents == null) throw new IOException("Cannot find expected contents property"); else objectMap = JsonUtil.getJsonObjects(contents); if (mdw != null) objectMap.put(".mdw", mdw); byte[] buffer = new byte[ZIP_BUFFER_KB * 1024]; ZipOutputStream zos = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { zos = new ZipOutputStream(outputStream); for (String name : objectMap.keySet()) { JSONObject json = objectMap.get(name); ZipEntry ze = new ZipEntry(name); zos.putNextEntry(ze); InputStream inputStream = new ByteArrayInputStream(json.toString(2).getBytes()); int len; while ((len = inputStream.read(buffer)) > 0) { zos.write(buffer, 0, len); } } } finally { if (zos != null) { zos.closeEntry(); zos.close(); } } byte[] bytes = outputStream.toByteArray(); return new String(Base64.encodeBase64(bytes)); }