Example usage for java.util.zip ZipOutputStream ZipOutputStream

List of usage examples for java.util.zip ZipOutputStream ZipOutputStream

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream ZipOutputStream.

Prototype

public ZipOutputStream(OutputStream out) 

Source Link

Document

Creates a new ZIP output stream.

Usage

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;//from w ww.j  av  a 2  s  .  co  m
        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: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  ww  . j  a v a 2 s. com
    }

    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:com.heliosdecompiler.appifier.Appifier.java

public static void main(String[] args) throws Throwable {
    if (args.length == 0) {
        System.out.println("An input JAR must be specified");
        return;/*from w w w  .j  ava2s.  c o  m*/
    }

    File in = new File(args[0]);

    if (!in.exists()) {
        System.out.println("Input not found");
        return;
    }

    String outName = args[0];
    outName = outName.substring(0, outName.length() - ".jar".length()) + "-appified.jar";

    File out = new File(outName);

    if (out.exists()) {
        if (!out.delete()) {
            System.out.println("Could not delete out file");
            return;
        }
    }

    try (ZipOutputStream outstream = new ZipOutputStream(new FileOutputStream(out));
            ZipFile zipFile = new ZipFile(in)) {

        {
            ZipEntry systemHook = new ZipEntry("com/heliosdecompiler/appifier/SystemHook.class");
            outstream.putNextEntry(systemHook);
            outstream.write(SystemHookDump.dump());
            outstream.closeEntry();
        }

        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();

        while (enumeration.hasMoreElements()) {
            ZipEntry next = enumeration.nextElement();

            if (!next.isDirectory()) {
                ZipEntry result = new ZipEntry(next.getName());
                outstream.putNextEntry(result);
                if (next.getName().endsWith(".class")) {
                    byte[] classBytes = IOUtils.toByteArray(zipFile.getInputStream(next));
                    outstream.write(transform(classBytes));
                } else {
                    IOUtils.copy(zipFile.getInputStream(next), outstream);
                }
                outstream.closeEntry();
            }
        }
    }
}

From source file:de.jwi.zip.Zipper.java

public static void main(String[] args) throws Exception {
    ZipOutputStream z = new ZipOutputStream(new FileOutputStream("d:/temp/myfirst.zip"));

    IOFileFilter filter = new IOFileFilter() {

        public boolean accept(java.io.File file) {
            return true;
        }/*from   w w w .j  a v a 2 s  . c  o m*/

        public boolean accept(java.io.File dir, java.lang.String name) {
            return true;
        }
    };

    Collection c = FileUtils.listFiles(new File("/java/javadocs/j2sdk-1.4.1/docs/tooldocs"), filter, filter);

    new Zipper().zip(z, c, new File("/java/javadocs/j2sdk-1.4.1"));

    z.close();

}

From source file:backup.BackupMaker.java

/**
 * @param args/*from ww  w. j a  v  a  2 s.  com*/
 * 
 * TOKEN WHAT_EXPORT [USER GROUPS TYPES MAX_SIZE] [OUTPUT_FOLDER]
 * 
 * WHAT_EXPORT Is a combination of "c" (Public Channels), 
 * "g" (Private Channels), "p" (Private Messages) and "f" (Files)
 * 
 * USER "all"/username, Only download files from a user
 * 
 * GROUPS "all" or a combination of "posts", "snippets", "images", 
 * "gdocs", "zips" and "pdfs". Use "|" as a separator. 
 * 
 * TYPES "all" or a combination of file types separated with "|". zip, mp4, etc. .
 * + at the beginning means to include them, - to exclude.
 * 
 * MAX_SIZE "none" or max size per file in bytes
 * 
 * OUTPUT_FOLDER Folder where to save the resultant .zip file. Must include a directory separator in the last character.
 */
public static void main(String[] args) {

    if (args.length == 1 && args[0].equals("--help"))
        showHelp();
    else if (args.length == 1 && args[0].equals("-h"))
        showShortHelp();
    else {
        FileOutputStream dest = null;
        ZipOutputStream out = null;
        try {
            BackupConfig config = new BackupConfig(args);
            dest = new FileOutputStream(destination(config.outputFolder, config.teamName));
            out = new ZipOutputStream(new BufferedOutputStream(dest));

            if (config.getPrivateMessages)
                backUpPrivateConversations(config, out);
            if (config.getPrivateChannels)
                backUpPrivateChannels(config, out);
            if (config.getPublicChannels)
                backUpPublicChannels(config, out);
            if (config.getFiles)
                backUpFiles(config, out);

            tryToCloseOutputStreams(out, dest);
        } catch (JSONException e) {
            tryToCloseOutputStreams(out, dest);
            System.err.println(e);
            ExitCodes.exit(ExitCodes.EX_SOFTWARE);
        } catch (IOException e) {
            tryToCloseOutputStreams(out, dest);
            System.err.println(e);
            ExitCodes.exit(ExitCodes.EX_IOERR);
        }
        System.out.println("SlackBackup finished successfully.");
    }

    ExitCodes.exit(ExitCodes.EX_OK);
}

From source file:Main.java

public static byte[] compressZip(byte bytes[]) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ZipOutputStream zipos = new ZipOutputStream(os);
    zipos.putNextEntry(new ZipEntry("ZIP"));
    zipos.write(bytes, 0, bytes.length);
    zipos.close();/*from ww w.j ava 2 s.c om*/
    return os.toByteArray();
}

From source file:Main.java

public static void compressFile(File file, File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    FileInputStream fileInputStream = new FileInputStream(file);
    zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

    int size;/*w  w w  . j av  a2 s .c o m*/
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

    zipOutputStream.closeEntry();
    fileInputStream.close();
    zipOutputStream.close();
}

From source file:Main.java

private static void zipDir(String zipFileName, String dir) throws Exception {
    File dirObj = new File(dir);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    System.out.println("Creating : " + zipFileName);
    addDir(dirObj, out);/*from  w  w w  .j av  a 2 s.com*/
    out.close();
}

From source file:Main.java

public static void compressFiles(File files[], File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    for (int i = 0; i < files.length; i++) {
        FileInputStream fileInputStream = new FileInputStream(files[i]);
        zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath()));

        int size;
        while ((size = fileInputStream.read(buffer)) > 0)
            zipOutputStream.write(buffer, 0, size);

        zipOutputStream.closeEntry();//w  ww .  ja  v  a2 s.  c om
        fileInputStream.close();
    }

    zipOutputStream.close();
}

From source file:Main.java

public static final void zipDirectory(File directory, File zip) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
    zip(directory, directory.getParentFile(), zos);
    zos.close();//from w w  w.j a  va2s .  c  o  m
}