List of usage examples for java.util.zip ZipOutputStream setMethod
public void setMethod(int method)
From source file:org.fusesource.meshkeeper.util.internal.FileSupport.java
public static void jar(File source, File target) throws IOException { ZipOutputStream os = new ZipOutputStream(new FileOutputStream(target)); try {//from w w w .j av a2 s . c om os.setMethod(ZipOutputStream.DEFLATED); os.setLevel(5); recusiveJar(os, source, null); } catch (IOException ioe) { IOException nioe = new IOException("Error jarring " + source); nioe.initCause(ioe); throw nioe; } finally { close(os); } }
From source file:com.edgenius.core.util.ZipFileUtil.java
/** * Creates a ZIP file and places it in the current working directory. The zip file is compressed * at the default compression level of the Deflater. * /*w w w. j a v a 2 s .c o m*/ * @param listToZip. Key is file or directory, value is parent directory which will remove from given file/directory because * compression only save relative directory. For example, c:\geniuswiki\data\repository\somefile, if value is c:\geniuswiki, then only * \data\repository\somefile will be saved. It is very important, the value must be canonical path, ie, c:\my document\geniuswiki, * CANNOT like this "c:\my doc~1\" * * */ public static void createZipFile(String zipFileName, Map<File, String> listToZip, boolean withEmptyDir) throws ZipFileUtilException { ZipOutputStream zop = null; try { zop = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName))); zop.setMethod(ZipOutputStream.DEFLATED); zop.setLevel(Deflater.DEFAULT_COMPRESSION); for (Entry<File, String> entry : listToZip.entrySet()) { File file = entry.getKey(); if (!file.exists()) { log.warn("Unable to find file " + file + " to zip"); continue; } if (file.isDirectory()) { Collection<File> list = FileUtils.listFiles(file, null, true); for (File src : list) { addEntry(zop, src, createRelativeDir(src.getCanonicalPath(), entry.getValue())); } if (withEmptyDir) { final List<File> emptyDirs = new ArrayList<File>(); if (file.list().length == 0) { emptyDirs.add(file); } else { //I just don't know how quickly to find out all empty sub directories recursively. so use below hack: FileUtils.listFiles(file, FileFilterUtils.falseFileFilter(), new IOFileFilter() { //JDK1.6 @Override public boolean accept(File f) { if (!f.isDirectory()) return false; int size = f.listFiles().length; if (size == 0) { emptyDirs.add(f); } return true; } //JDK1.6 @Override public boolean accept(File arg0, String arg1) { return true; } }); } for (File src : emptyDirs) { addEntry(zop, null, createRelativeDir(src.getCanonicalPath(), entry.getValue())); } } } else { addEntry(zop, file, createRelativeDir(file.getCanonicalPath(), entry.getValue())); } } } catch (IOException e1) { throw new ZipFileUtilException( "An error has occurred while trying to zip the files. Error message is: ", e1); } finally { try { if (zop != null) zop.close(); } catch (Exception e) { } } }
From source file:com.l2jfree.sql.L2DataSource.java
protected static final boolean writeBackup(String databaseName, InputStream in) throws IOException { FileUtils.forceMkdir(new File("backup/database")); final Date time = new Date(); final L2TextBuilder tb = new L2TextBuilder(); tb.append("backup/database/DatabaseBackup_"); tb.append(new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date())); tb.append("_uptime-").append(L2Config.getShortUptime()); tb.append(".zip"); final File backupFile = new File(tb.moveToString()); int written = 0; ZipOutputStream out = null; try {/*from w w w . j a v a 2 s . co m*/ out = new ZipOutputStream(new FileOutputStream(backupFile)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); out.setComment("L2jFree Schema Backup Utility\r\n\r\nBackup date: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(new Date())); out.putNextEntry(new ZipEntry(databaseName + ".sql")); byte[] buf = new byte[4096]; for (int read; (read = in.read(buf)) != -1;) { out.write(buf, 0, read); written += read; } } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } if (written == 0) { backupFile.delete(); return false; } _log.info("DatabaseBackupManager: Database `" + databaseName + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000 + " s."); return true; }
From source file:org.opensha.commons.util.FileUtils.java
/** * This function creates a Zip file called "allFiles.zip" for all the * files that exist in filesPath.//from ww w . ja v a 2 s . c om * @param filesPath String Folder with absolute path in zip file will be created. * This function searches for all the files in the folder "filesPath" and adds * those to a single zip file "allFiles.zip". */ public static void createZipFile(String filesPath) { int BUFFER = 8192; String zipFileName = "allFiles.zip"; if (!filesPath.endsWith(SystemUtils.FILE_SEPARATOR)) filesPath = filesPath + SystemUtils.FILE_SEPARATOR; try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(filesPath + zipFileName); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); out.setMethod(ZipOutputStream.DEFLATED); byte data[] = new byte[BUFFER]; // get a list of files from current directory File f = new File(filesPath); String files[] = f.list(); for (int i = 0; i < files.length; i++) { if (files[i].equals(zipFileName)) continue; System.out.println("Adding: " + files[i]); FileInputStream fi = new FileInputStream(filesPath + files[i]); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(files[i]); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java
public static void pack(final File archiveTarget, final File packDir, final IFileFilter filter) throws ZipException, IOException { if (!packDir.isDirectory()) { return;/*from w w w.ja v a 2 s. c om*/ } final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(archiveTarget)); final ZipOutputStream out = new ZipOutputStream(bos); out.setMethod(ZipOutputStream.DEFLATED); final File[] files = packDir.listFiles(); for (final File file : files) { processFiles(packDir, file, out, filter); } out.close(); bos.flush(); bos.close(); }
From source file:it.cnr.icar.eric.common.Utility.java
public static ZipOutputStream createZipOutputStream(String baseDir, String[] relativeFilePaths, OutputStream os) throws FileNotFoundException, IOException { if (baseDir.startsWith("file:/")) { baseDir = baseDir.substring(5);//from w ww.j a va 2 s . c o m } ZipOutputStream zipoutputstream = new ZipOutputStream(os); zipoutputstream.setMethod(ZipOutputStream.STORED); for (int i = 0; i < relativeFilePaths.length; i++) { File file = new File(baseDir + FILE_SEPARATOR + relativeFilePaths[i]); byte[] buffer = new byte[1000]; int n; FileInputStream fis; // Calculate the CRC-32 value. This isn't strictly necessary // for deflated entries, but it doesn't hurt. CRC32 crc32 = new CRC32(); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { crc32.update(buffer, 0, n); } fis.close(); // Create a zip entry. ZipEntry zipEntry = new ZipEntry(relativeFilePaths[i]); zipEntry.setSize(file.length()); zipEntry.setTime(file.lastModified()); zipEntry.setCrc(crc32.getValue()); // Add the zip entry and associated data. zipoutputstream.putNextEntry(zipEntry); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { zipoutputstream.write(buffer, 0, n); } fis.close(); zipoutputstream.closeEntry(); } return zipoutputstream; }
From source file:org.xwoot.xwootUtil.FileUtil.java
/** * Zip an array of files. Each file in the array must reflect a file on disk. If a file represents a directory on * disk, it will be skipped./*from w ww. j a va2s .c o m*/ * * @param files the files to be zipped. * @param zippedFileDestinationPath the location of the zip file that will result. * @throws IOException if problems occur. */ public static void zipFiles(File[] files, String zippedFileDestinationPath) throws IOException { if (files == null || zippedFileDestinationPath == null) { throw new NullPointerException("Null paramenters. filePaths: " + files + " zippedFileDestinationPath: " + zippedFileDestinationPath); } FileUtil.checkDirectoryPath(new File(zippedFileDestinationPath).getParent()); if (files.length < 1) { return; } FileOutputStream fos = null; BufferedOutputStream bos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zippedFileDestinationPath); bos = new BufferedOutputStream(fos); zos = new ZipOutputStream(fos); zos.setMethod(ZipOutputStream.DEFLATED); zos.setLevel(Deflater.BEST_COMPRESSION); for (File aFile : files) { zipFiletoZipOutputStream(aFile.getParentFile(), aFile.getName(), zos); } } catch (Exception e) { throw new IOException("Failed to zip files: (" + e.getClass() + ") " + e.getMessage()); } finally { try { if (zos != null) { zos.close(); } if (bos != null) { bos.close(); } if (fos != null) { fos.close(); } } catch (Exception e) { throw new IOException("Problem closing streams. Reason: " + e.getMessage()); } } }
From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java
public void create(boolean noCompress) throws IOException { Logger.info("Generating zpak file from directory %s with compression = %b", mDirectory.getPath(), !noCompress);//from w w w . ja va2 s . co m IOFileFilter filter = new IOFileFilter() { public boolean accept(File file) { if (file.getName().startsWith(".")) { Logger.debug("Skipping file %s", file.getPath()); return false; } return true; } public boolean accept(File file, String s) { if (s.startsWith(".")) { Logger.debug("Skipping file %s", file.getPath()); return false; } return true; } }; Iterator<File> it = FileUtils.iterateFiles(mDirectory, filter, TrueFileFilter.INSTANCE); ZipOutputStream out = new ZipOutputStream(mOut); out.setMethod(noCompress ? ZipEntry.STORED : ZipEntry.DEFLATED); while (it.hasNext()) { File current = it.next(); FileInputStream in = new FileInputStream(current); ZipEntry zEntry = new ZipEntry( current.getPath().replace(mDirectory.getPath(), "").substring(1).replace("\\", "/")); if (noCompress) { zEntry.setSize(in.getChannel().size()); zEntry.setCompressedSize(in.getChannel().size()); zEntry.setCrc(getCRC32(current)); } out.putNextEntry(zEntry); Logger.verbose("Adding file %s", current.getPath()); int n; while ((n = in.read(mBuffer)) != -1) { out.write(mBuffer, 0, n); } in.close(); out.closeEntry(); } out.close(); Logger.debug("Done with ZPAK creation."); }
From source file:org.junitee.testngee.servlet.TestNGEEServlet.java
private void zipOutputDir(File outdir, ServletOutputStream outputStream) throws IOException { ZipOutputStream out = new ZipOutputStream(outputStream); File[] files = outdir.listFiles(); out.setMethod(ZipOutputStream.DEFLATED); for (int i = 0; i < files.length; i++) { File file = files[i];/*from w ww . ja v a2s . c o m*/ if (!file.isDirectory()) { ZipEntry zipEntry = new ZipEntry(file.getName()); zipEntry.setSize(file.length()); zipEntry.setTime(file.lastModified()); out.putNextEntry(zipEntry); FileInputStream in = new FileInputStream(file); byte[] buffer = new byte[4096]; int r; while ((r = in.read(buffer)) > 0) { out.write(buffer, 0, r); } in.close(); out.closeEntry(); } } out.close(); }
From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java
/** * Compress data//from ww w .j a v a2 s . c om * * @param fileCompressor * FileCompressor object * @return * @throws Exception */ @Override public byte[] compressData(FileCompressor fileCompressor) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(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(); }