List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:com.baasbox.util.Util.java
public static void createZipFile(String path, File... files) { if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("Zipping into:" + path); ZipOutputStream zip = null;/*ww w. j a va 2s.c o m*/ FileOutputStream dest = null; try { File f = new File(path); dest = new FileOutputStream(f); zip = new ZipOutputStream(new BufferedOutputStream(dest)); for (File file : files) { zip.putNextEntry(new ZipEntry(file.getName())); zip.write(FileUtils.readFileToByteArray(file)); zip.closeEntry(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Unable to create zip file"); } finally { try { if (zip != null) zip.close(); if (dest != null) dest.close(); } catch (Exception ioe) { //Nothing to do } } }
From source file:md.archivers.ZIPArchiver.java
private void zipDirectory(File zipFile, File directory) throws FileNotFoundException, IOException { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); FileInputStream fin = null;/* ww w . j a va 2 s. co m*/ for (File d : directory.listFiles()) { for (File f : d.listFiles()) { zos.putNextEntry(new ZipEntry(createNameForZip(f))); try { fin = new FileInputStream(f); IOUtils.copy(fin, zos); } finally { if (fin != null) fin.close(); } zos.closeEntry(); } } zos.close(); }
From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java
private static void storeOpenDocumentSpreadsheetFile(File file, InputStream source) throws Exception { try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { //addResourceFile("mimetype", "/resource/ods/mimetype", out); ZipEntry ze = new ZipEntry("mimetype"); String mime = "application/vnd.oasis.opendocument.spreadsheet"; ze.setMethod(ZipEntry.STORED); ze.setSize(mime.length());/* ww w.j av a 2 s . c o m*/ CRC32 crc = new CRC32(); crc.update(mime.getBytes()); ze.setCrc(crc.getValue()); out.putNextEntry(ze); for (int i = 0; i < mime.length(); i++) { out.write(mime.charAt(i)); } out.closeEntry(); ZipEntry zipEntry = new ZipEntry("content.xml"); //zipEntry.setMethod(ZipEntry.DEFLATED); out.putNextEntry(zipEntry); int c; while ((c = source.read()) >= 0) { out.write(c); } out.closeEntry(); // Add manifest (required for OOo 2.0) and "meta.xml": These are in the // resource/ods directory, and are copied verbatim into the zip file. OpenDocumentSpreadsheetCreator.addResourceFile("meta.xml", "/resource/ods/meta.xml", out); OpenDocumentSpreadsheetCreator.addResourceFile("META-INF/manifest.xml", "/resource/ods/manifest.xml", out); } }
From source file:io.inkstand.it.StaticContentITCase.java
private static File createZip(final File file, final String... resources) throws IOException { try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream((file)))) { for (String resourceName : resources) { final InputStream is = StaticContentITCase.class.getResourceAsStream(resourceName); final ZipEntry zipEntry = new ZipEntry(resourceName); zos.putNextEntry(zipEntry);/* w w w . j a v a2 s. c o m*/ IOUtils.copy(is, zos); zos.closeEntry(); } } return file; }
From source file:ml.shifu.shifu.util.IndependentTreeModelUtils.java
public boolean convertBinaryToZipSpec(File treeModelFile, File outputZipFile) { FileInputStream treeModelInputStream = null; ZipOutputStream zipOutputStream = null; try {/*from w w w .ja v a 2 s.c o m*/ treeModelInputStream = new FileInputStream(treeModelFile); IndependentTreeModel treeModel = IndependentTreeModel.loadFromStream(treeModelInputStream); List<List<TreeNode>> trees = treeModel.getTrees(); treeModel.setTrees(null); if (CollectionUtils.isEmpty(trees)) { logger.error("No trees found in the tree model."); return false; } zipOutputStream = new ZipOutputStream(new FileOutputStream(outputZipFile)); ZipEntry modelEntry = new ZipEntry(MODEL_CONF); zipOutputStream.putNextEntry(modelEntry); ByteArrayOutputStream byos = new ByteArrayOutputStream(); JSONUtils.writeValue(new OutputStreamWriter(byos), treeModel); zipOutputStream.write(byos.toByteArray()); IOUtils.closeQuietly(byos); ZipEntry treesEntry = new ZipEntry(MODEL_TREES); zipOutputStream.putNextEntry(treesEntry); DataOutputStream dataOutputStream = new DataOutputStream(zipOutputStream); dataOutputStream.writeInt(trees.size()); for (List<TreeNode> forest : trees) { dataOutputStream.writeInt(forest.size()); for (TreeNode treeNode : forest) { treeNode.write(dataOutputStream); } } IOUtils.closeQuietly(dataOutputStream); } catch (IOException e) { logger.error("Error occurred when convert the tree model to zip format.", e); return false; } finally { IOUtils.closeQuietly(zipOutputStream); IOUtils.closeQuietly(treeModelInputStream); } return true; }
From source file:de.micromata.genome.gdbfs.ZipWriteFileSystem.java
@Override public void writeBinaryFile(String file, InputStream is, boolean overWrite) { ZipEntry ze = new ZipEntry(file); try {/*from ww w . ja v a 2 s. c o m*/ // if (source.isFile() == true) { zout.putNextEntry(ze); IOUtils.copy(is, zout); zout.closeEntry(); } catch (IOException ex) { throw new RuntimeIOException(ex); } }
From source file:be.fedict.eid.dss.document.zip.ZIPSignatureOutputStream.java
@Override public void close() throws IOException { super.close(); byte[] signatureData = toByteArray(); /*/* w ww.j av a 2 s . c o m*/ * Copy the original ZIP content. */ ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream); ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile)); ZipEntry zipEntry; while (null != (zipEntry = zipInputStream.getNextEntry())) { if (!zipEntry.getName().equals(ODFUtil.SIGNATURE_FILE)) { ZipEntry newZipEntry = new ZipEntry(zipEntry.getName()); zipOutputStream.putNextEntry(newZipEntry); LOG.debug("copying " + zipEntry.getName()); IOUtils.copy(zipInputStream, zipOutputStream); } } zipInputStream.close(); /* * Add the XML signature file to the ZIP package. */ zipEntry = new ZipEntry(ODFUtil.SIGNATURE_FILE); LOG.debug("writing " + zipEntry.getName()); zipOutputStream.putNextEntry(zipEntry); IOUtils.write(signatureData, zipOutputStream); zipOutputStream.close(); }
From source file:br.univali.celine.lms.utils.zip.Zip.java
public void zip(ArrayList<String> fileList, File destFile, int compressionLevel) throws Exception { if (destFile.exists()) throw new Exception("File " + destFile.getName() + " already exists!!"); int fileLength; byte[] buffer = new byte[4096]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zipFile = new ZipOutputStream(bos); zipFile.setLevel(compressionLevel);/* w ww . j a va 2 s .com*/ for (int i = 0; i < fileList.size(); i++) { FileInputStream fis = new FileInputStream(fileList.get(i)); BufferedInputStream bis = new BufferedInputStream(fis); ZipEntry ze = new ZipEntry(FilenameUtils.getName(fileList.get(i))); zipFile.putNextEntry(ze); while ((fileLength = bis.read(buffer, 0, 4096)) > 0) zipFile.write(buffer, 0, fileLength); zipFile.closeEntry(); bis.close(); } zipFile.close(); }
From source file:com.espringtran.compressor4j.processor.GzipProcessor.java
/** * Compress data//from ww w. java 2s.com * * @param fileCompressor * FileCompressor object * @return * @throws Exception */ @Override public byte[] compressData(FileCompressor fileCompressor) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos); ZipOutputStream zos = new ZipOutputStream(cos); try { zos.setLevel(fileCompressor.getLevel().getValue()); zos.setMethod(ZipOutputStream.DEFLATED); zos.setComment(fileCompressor.getComment()); for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) { zos.putNextEntry(new ZipEntry(binaryFile.getDesPath())); zos.write(binaryFile.getData()); zos.closeEntry(); } zos.flush(); zos.finish(); } catch (Exception e) { FileCompressor.LOGGER.error("Error on compress data", e); } finally { zos.close(); cos.close(); baos.close(); } return baos.toByteArray(); }
From source file:com.nuvolect.securesuite.util.OmniZip.java
private static void zipSubDirectory(Context ctx, String basePath, OmniFile dir, ZipOutputStream zos) throws IOException { LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipSubDirectory : " + dir.getPath()); OmniFile[] files = dir.listFiles();/* w w w .java 2 s . c o m*/ for (OmniFile file : files) { if (file.isDirectory()) { String path = basePath + file.getName() + File.separator; zos.putNextEntry(new ZipEntry(path)); zipSubDirectory(ctx, path, file, zos); zos.closeEntry(); } else { if (file.isStd())// don't scan crypto volume MediaScannerConnection.scanFile(ctx, new String[] { file.getAbsolutePath() }, null, null); zipFile(basePath, file, zos); } } }