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 void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    String outputFile = "a.zip";
    int level = 9;
    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);/*from w ww .jav  a 2  s  . c  o m*/

    ZipEntry ze = new ZipEntry("a.zip");
    FileInputStream fin = new FileInputStream("b.dat");
    zout.putNextEntry(ze);
    for (int c = fin.read(); c != -1; c = fin.read()) {
        zout.write(c);
    }
    fin.close();
    zout.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String outputFile = "new.zip";
    int level = 9;
    int start = 1;

    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);/*from ww w . j  av  a2  s.  c  om*/
    for (int i = start; i < args.length; i++) {
        ZipEntry ze = new ZipEntry(args[i]);
        FileInputStream fin = new FileInputStream(args[i]);
        try {
            System.out.println("Compressing " + args[i]);
            zout.putNextEntry(ze);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                zout.write(c);
            }
        } finally {
            fin.close();
        }
    }
    zout.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    String outputFile = "new.zip";
    // Default to maximum compression
    int level = 9;
    int start = 1;

    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);//from  www .jav  a2s  .c o  m
    for (int i = start; i < args.length; i++) {
        ZipEntry ze = new ZipEntry(args[i]);
        FileInputStream fin = new FileInputStream(args[i]);
        try {
            System.out.println("Compressing " + args[i]);
            zout.putNextEntry(ze);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                zout.write(c);
            }
        } finally {
            fin.close();
        }
    }
    zout.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   w  w w . ja  v a 2s  . co  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:nl.clockwork.common.util.Utils.java

public static byte[] zip(String name, String content) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipOutputStream zout = new ZipOutputStream(out);
    ZipEntry entry = new ZipEntry(name);
    zout.putNextEntry(entry);/*from   www .j a  va2s .c om*/
    zout.write(content.getBytes());
    zout.close();
    return out.toByteArray();
}

From source file:com.ikanow.aleph2.analytics.spark.utils.SparkPyTechnologyUtils.java

/** Create a zip containing the Aleph2 driver
 * @param bucket/*from ww w  .  j  av  a  2  s .com*/
 * @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:org.eclipse.recommenders.utils.Zips.java

/**
 * Appends the given data to the zip output stream using the specified path.
 *///from w  w w. j av  a 2s . c  o m
public static void append(ZipOutputStream zos, String path, String data) throws IOException {
    ZipEntry e = new ZipEntry(path);
    zos.putNextEntry(e);
    zos.write(data.getBytes(Charsets.UTF_8));
    zos.closeEntry();
}

From source file:ee.ria.xroad.common.asic.AsicHelper.java

private static void addEntry(ZipOutputStream zip, String name, byte[] data) throws IOException {
    zip.putNextEntry(new ZipEntry(name));
    zip.write(data);
}

From source file:com.hbm.devices.scan.ui.android.DeviceZipper.java

static Uri saveAnnounces(@NonNull List<Announce> announces, @NonNull AppCompatActivity activity) {
    final TimeZone tz = TimeZone.getTimeZone("UTC");
    final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US);
    df.setTimeZone(tz);//from w  w w. j  a v  a2 s .co m
    final String isoDate = df.format(new Date());
    final Charset charSet = Charset.forName("UTF-8");

    try {
        final File file = createFile(activity);
        if (file == null) {
            return null;
        }
        final FileOutputStream fos = new FileOutputStream(file, false);
        final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
        final ZipEntry entry = new ZipEntry("devices.json");
        zos.putNextEntry(entry);
        zos.write(("{\"date\":\"" + isoDate + "\",").getBytes(charSet));
        zos.write(("\"version\": \"1.0\",").getBytes(charSet));
        zos.write("\"devices\":[".getBytes(charSet));

        final Iterator<Announce> iterator = announces.iterator();
        while (iterator.hasNext()) {
            final Announce announce = iterator.next();
            zos.write(announce.getJSONString().getBytes(charSet));
            if (iterator.hasNext()) {
                zos.write(",\n".getBytes(charSet));
            }
        }

        zos.write("]}".getBytes(charSet));
        zos.closeEntry();
        zos.close();
        fos.close();
        return FileProvider.getUriForFile(activity, "com.hbm.devices.scan.ui.android.fileprovider", file);
    } catch (IOException e) {
        Toast.makeText(activity, activity.getString(R.string.could_not_create, e), Toast.LENGTH_SHORT).show();
        return null;
    }
}

From source file:Main.java

private static byte[] createZip(Map<String, byte[]> files) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zf = new ZipOutputStream(bos);
    Iterator<String> it = files.keySet().iterator();
    String fileName = null;/*  ww  w.j  ava 2 s  .c  om*/
    ZipEntry ze = null;

    while (it.hasNext()) {
        fileName = it.next();
        ze = new ZipEntry(fileName);
        zf.putNextEntry(ze);
        zf.write(files.get(fileName));
    }
    zf.close();

    return bos.toByteArray();
}