Example usage for java.util.zip ZipOutputStream write

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

Introduction

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

Prototype

public synchronized void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes an array of bytes to the current ZIP entry data.

Usage

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);
    }//  w w  w  .  j a  v  a 2  s.  c om

    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;/*from w ww.j ava2s.  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 {
    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 ww  w.j a v a2 s  . c o  m

    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  ww  w. j  a v  a2s .  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

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   w  ww.  j  av a 2 s .  c  o m
    return os.toByteArray();
}

From source file:org.exist.util.Compressor.java

/**
 * The method <code>compress</code>
 *
 * @param whatToCompress a <code>byte[]</code> value
 * @param length an <code>int</code> value
 * @return a <code>byte[]</code> value
 * @exception IOException if an error occurs
 *///  w  w w . ja  v  a 2  s  . co  m
public static byte[] compress(byte[] whatToCompress, int length) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ZipOutputStream gzos = new ZipOutputStream(baos);
    gzos.setMethod(ZipOutputStream.DEFLATED);
    gzos.putNextEntry(new ZipEntry(length + ""));
    gzos.write(whatToCompress, 0, length);
    gzos.closeEntry();
    gzos.finish();
    gzos.close();
    return baos.toByteArray();
}

From source file:co.cask.hydrator.transforms.Compressor.java

public static byte[] compressZIP(byte[] input) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(out);
    zip.write(input, 0, input.length);
    zip.close();/*from   w w w.  j  av  a2 s  .  c  o  m*/
    return out.toByteArray();
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static void writeZipEntry(ZipOutputStream out, String name, byte[] data) throws IOException {
    final ZipEntry entry = new ZipEntry(StringUtils.removeStart(name, "/"));
    out.putNextEntry(entry);/*from ww w  .j  av  a 2  s. c  o m*/
    out.write(data, 0, data.length);
    out.closeEntry();
}

From source file:Main.java

public static void addEntryContent(ZipOutputStream zos, String entryFileName)
        throws IOException, FileNotFoundException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFileName));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = bis.read(buffer)) != -1) {
        zos.write(buffer, 0, count);
    }/*from w  w w  . j ava  2s.c  o  m*/
    bis.close();
}

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;//from  w  ww  .j a v a2 s.  co m
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

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