List of usage examples for java.util.zip ZipOutputStream closeEntry
public void closeEntry() throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("C:/MyZip.zip"); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry("C:/file1.txt"); zos.putNextEntry(ze);//from w ww . j a va 2s . c o m zos.closeEntry(); ze = new ZipEntry("C:/file2.txt"); zos.putNextEntry(ze); zos.closeEntry(); zos.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileInputStream inStream = new FileInputStream("test.txt"); ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream("compressed.zip")); outStream.putNextEntry(new ZipEntry("test.txt")); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, bytesRead); }//from w ww. ja v a 2 s . c o m outStream.closeEntry(); outStream.close(); inStream.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String[] filenames = new String[] { "filename1", "filename2" }; byte[] buf = new byte[1024]; String outFilename = "outfile.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); for (int i = 0; i < filenames.length; i++) { FileInputStream in = new FileInputStream(filenames[i]); out.putNextEntry(new ZipEntry(filenames[i])); int len;// w w w . j a v a2s . c o m while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0]))); for (int n = 1; n < args.length; n++) { BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n])); ZipEntry ze = new ZipEntry(rmPath(args[n])); fout.putNextEntry(ze);/*from ww w. ja v a 2 s . c o m*/ int i; do { i = fin.read(); if (i != -1) fout.write(i); } while (i != -1); fout.closeEntry(); fin.close(); System.out.println("Compressing " + args[n]); System.out.println( " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n"); } fout.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { CheckedOutputStream checksum = new CheckedOutputStream(new FileOutputStream("data.zip"), new Adler32()); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum)); int size = 0; byte[] buffer = new byte[1024]; File dir = new File("."); String[] files = dir.list();//from w w w. j a va 2 s . c om for (int i = 0; i < files.length; i++) { System.out.println("Compressing: " + files[i]); FileInputStream fis = new FileInputStream(files[i]); ZipEntry zipEntry = new ZipEntry(files[i]); zos.putNextEntry(zipEntry); while ((size = fis.read(buffer, 0, buffer.length)) > 0) { zos.write(buffer, 0, size); } zos.closeEntry(); fis.close(); } zos.close(); System.out.println("Checksum : " + checksum.getChecksum().getValue()); }
From source file:com.alexoree.jenkins.Main.java
public static void main(String[] args) throws Exception { // create Options object Options options = new Options(); options.addOption("t", false, "throttle the downloads, waits 5 seconds in between each d/l"); // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("jenkins-sync", options); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); boolean throttle = cmd.hasOption("t"); String plugins = "https://updates.jenkins-ci.org/latest/"; List<String> ps = new ArrayList<String>(); Document doc = Jsoup.connect(plugins).get(); for (Element file : doc.select("td a")) { //System.out.println(file.attr("href")); if (file.attr("href").endsWith(".hpi") || file.attr("href").endsWith(".war")) { ps.add(file.attr("href")); }/*from w w w . j a v a2 s. co m*/ } File root = new File("."); //https://updates.jenkins-ci.org/latest/AdaptivePlugin.hpi new File("./latest").mkdirs(); //output zip file String zipFile = "jenkinsSync.zip"; // create byte buffer byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); //download the plugins for (int i = 0; i < ps.size(); i++) { System.out.println("[" + i + "/" + ps.size() + "] downloading " + plugins + ps.get(i)); String outputFile = download(root.getAbsolutePath() + "/latest/" + ps.get(i), plugins + ps.get(i)); FileInputStream fis = new FileInputStream(outputFile); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(outputFile.replace(root.getAbsolutePath(), "") .replace("updates.jenkins-ci.org/", "").replace("https:/", ""))); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); if (throttle) Thread.sleep(WAIT); new File(root.getAbsolutePath() + "/latest/" + ps.get(i)).deleteOnExit(); } //download the json metadata plugins = "https://updates.jenkins-ci.org/"; ps = new ArrayList<String>(); doc = Jsoup.connect(plugins).get(); for (Element file : doc.select("td a")) { //System.out.println(file.attr("href")); if (file.attr("href").endsWith(".json")) { ps.add(file.attr("href")); } } for (int i = 0; i < ps.size(); i++) { download(root.getAbsolutePath() + "/" + ps.get(i), plugins + ps.get(i)); FileInputStream fis = new FileInputStream(root.getAbsolutePath() + "/" + ps.get(i)); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(plugins + ps.get(i))); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); new File(root.getAbsolutePath() + "/" + ps.get(i)).deleteOnExit(); if (throttle) Thread.sleep(WAIT); } // close the ZipOutputStream zos.close(); }
From source file:Main.java
/** * Equivalent to {@link ZipOutputStream#closeEntry()} but without checked * exception.//from w w w . j a v a 2 s . c om */ public static void closeEntry(ZipOutputStream outputStream) { try { outputStream.closeEntry(); } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void doEntry(Context context, ZipOutputStream zout, int resId, String dest) throws Exception { InputStream in = null;/*from w w w .ja v a 2 s . c o m*/ try { zout.putNextEntry(new ZipEntry(dest)); copyStream(in = context.getResources().openRawResource(resId), zout); } finally { zout.closeEntry(); in.close(); } }
From source file:com.ikanow.aleph2.analytics.spark.utils.SparkPyTechnologyUtils.java
/** Create a zip containing the Aleph2 driver * @param bucket/* w w w. j a va2 s .c om*/ * @param job * @throws IOException */ public static String writeAleph2DriverZip(final String signature) throws IOException { final String tmp_dir = System.getProperty("java.io.tmpdir"); final String filename = tmp_dir + "/aleph2_driver_py_" + signature + ".zip"; final InputStream io_stream = Thread.currentThread().getContextClassLoader() .getResourceAsStream("aleph2_driver.py"); final FileOutputStream fout = new FileOutputStream(filename); final ZipOutputStream zout = new ZipOutputStream(fout); final ZipEntry ze = new ZipEntry("aleph2_driver.py"); zout.putNextEntry(ze); zout.write(IOUtils.toString(io_stream).getBytes()); zout.closeEntry(); zout.close(); return filename; }
From source file:net.sf.jabref.exporter.OpenOfficeDocumentCreator.java
private static void addResourceFile(String name, String resource, ZipOutputStream out) throws IOException { ZipEntry zipEntry = new ZipEntry(name); out.putNextEntry(zipEntry);/*from w w w. ja va2 s.c o m*/ OpenOfficeDocumentCreator.addFromResource(resource, out); out.closeEntry(); }