List of usage examples for java.util.zip ZipOutputStream setComment
public void setComment(String comment)
From source file:Main.java
public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A test of Java Zipping"); for (int i = 0; i < args.length; i++) { System.out.println("Writing file " + args[i]); BufferedReader in = new BufferedReader(new FileReader(args[i])); zos.putNextEntry(new ZipEntry(args[i])); int c;//ww w .j av a2s . c o m while ((c = in.read()) != -1) out.write(c); in.close(); } out.close(); System.out.println("Checksum: " + csum.getChecksum().getValue()); System.out.println("Reading file"); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in2 = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in2); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file " + ze); int x; while ((x = bis.read()) != -1) System.out.write(x); } System.out.println("Checksum: " + csumi.getChecksum().getValue()); bis.close(); }
From source file:ZipCompress.java
public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A test of Java Zipping"); // No corresponding getComment(), though. for (int i = 0; i < args.length; i++) { System.out.println("Writing file " + args[i]); BufferedReader in = new BufferedReader(new FileReader(args[i])); zos.putNextEntry(new ZipEntry(args[i])); int c;// w w w. j a v a2 s . c om while ((c = in.read()) != -1) out.write(c); in.close(); } out.close(); // Checksum valid only after the file has been closed! System.out.println("Checksum: " + csum.getChecksum().getValue()); // Now extract the files: System.out.println("Reading file"); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in2 = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in2); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file " + ze); int x; while ((x = bis.read()) != -1) System.out.write(x); } System.out.println("Checksum: " + csumi.getChecksum().getValue()); bis.close(); // Alternative way to open and read zip files: ZipFile zf = new ZipFile("test.zip"); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); System.out.println("File: " + ze2); // ... and extract the data as before } }
From source file:ee.ria.xroad.common.asic.AsicHelper.java
static void write(AsicContainer asic, ZipOutputStream zip) throws Exception { zip.setComment("mimetype=" + MIMETYPE); for (Object expectedEntry : AsicContainerEntries.getALL_ENTRIES()) { String name;//from www . j av a 2 s . c o m if (expectedEntry instanceof String) { name = (String) expectedEntry; } else if (expectedEntry instanceof Pattern) { name = ENTRY_SIGNATURE; } else { continue; } String data = asic.get(name); if (data != null) { if (ENTRY_TIMESTAMP.equalsIgnoreCase(name)) { // If the timestamp is batch timestamp, add the timestamp.tst // to the container, else the timestamp is in the signature if (asic.getTimestamp() != null) { byte[] binary = decodeBase64(data); addEntry(zip, name, binary); } } else { addEntry(zip, name, data); } } } }
From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java
public static void makeBackup() { File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH); if (!f.mkdirs() && !f.exists()) { _log.warn("Could not create folder " + f.getAbsolutePath()); return;// ww w .j a va 2 s. c om } _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`..."); Process run = null; try { run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password=" + Config.DATABASE_PASSWORD + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers " + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH)); } catch (Exception e) { } finally { if (run == null) { _log.warn("Could not execute mysqldump!"); return; } } try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); Date time = new Date(); File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql")); if (!bf.createNewFile()) throw new IOException("Cannot create backup file: " + bf.getCanonicalPath()); InputStream input = run.getInputStream(); OutputStream out = new FileOutputStream(bf); if (Config.DATABASE_BACKUP_COMPRESSION) { ZipOutputStream dflt = new ZipOutputStream(out); dflt.setMethod(ZipOutputStream.DEFLATED); dflt.setLevel(Deflater.BEST_COMPRESSION); dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time)); dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql")); out = dflt; } byte[] buf = new byte[4096]; int written = 0; for (int read; (read = input.read(buf)) != -1;) { out.write(buf, 0, read); written += read; } input.close(); out.close(); if (written == 0) { bf.delete(); BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream())); String line; while ((line = br.readLine()) != null) _log.warn("DatabaseBackupManager: " + line); br.close(); } else _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000 + " s."); run.waitFor(); } catch (Exception e) { _log.warn("DatabaseBackupManager: Could not make backup: ", e); } }
From source file:edu.umd.cs.submit.CommandLineSubmit.java
/** * @param p//from ww w . java2 s .c om * @param find * @param files * @param userProps * @return * @throws IOException * @throws FileNotFoundException */ public static MultipartPostMethod createFilePost(Properties p, FindAllFiles find, Collection<File> files, Properties userProps) throws IOException, FileNotFoundException { // ========================== assemble zip file in byte array // ============================== String loginName = userProps.getProperty("loginName"); String classAccount = userProps.getProperty("classAccount"); String from = classAccount; if (loginName != null && !loginName.equals(classAccount)) from += "/" + loginName; System.out.println(" submitted by " + from); System.out.println(); System.out.println("Submitting the following files"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096); byte[] buf = new byte[4096]; ZipOutputStream zipfile = new ZipOutputStream(bytes); zipfile.setComment("zipfile for CommandLineTurnin, version " + VERSION); for (File resource : files) { if (resource.isDirectory()) continue; String relativePath = resource.getCanonicalPath().substring(find.rootPathLength + 1); System.out.println(relativePath); ZipEntry entry = new ZipEntry(relativePath); entry.setTime(resource.lastModified()); zipfile.putNextEntry(entry); InputStream in = new FileInputStream(resource); try { while (true) { int n = in.read(buf); if (n < 0) break; zipfile.write(buf, 0, n); } } finally { in.close(); } zipfile.closeEntry(); } // for each file zipfile.close(); MultipartPostMethod filePost = new MultipartPostMethod(p.getProperty("submitURL")); p.putAll(userProps); // add properties for (Map.Entry<?, ?> e : p.entrySet()) { String key = (String) e.getKey(); String value = (String) e.getValue(); if (!key.equals("submitURL")) filePost.addParameter(key, value); } filePost.addParameter("submitClientTool", "CommandLineTool"); filePost.addParameter("submitClientVersion", VERSION); byte[] allInput = bytes.toByteArray(); filePost.addPart(new FilePart("submittedFiles", new ByteArrayPartSource("submit.zip", allInput))); return filePost; }
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 ww w.j a v a2 s. c o 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:com.espringtran.compressor4j.processor.Bzip2Processor.java
/** * Compress data//from ww w. j a va 2 s. co m * * @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(); }
From source file:com.espringtran.compressor4j.processor.GzipProcessor.java
/** * Compress data/*from w w w . j a va 2 s. c o m*/ * * @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:ZipImploder.java
protected void configure(ZipOutputStream zos, String comment, int method, int level) { if (comment != null) { zos.setComment(comment); }/*from w w w . ja v a 2s . c o m*/ if (method >= 0) { zos.setMethod(method); } if (level >= 0) { zos.setLevel(level); } }
From source file:eu.europa.esig.dss.asic.signature.ASiCService.java
private void storeZipComment(final ASiCParameters asicParameters, final ZipOutputStream outZip, final String toSignDocumentName) { if (asicParameters.isZipComment() && StringUtils.isNotEmpty(toSignDocumentName)) { outZip.setComment("mimetype=" + getMimeTypeBytes(asicParameters)); }// w w w . j a v a 2 s. c o m }