Example usage for java.util.zip CheckedOutputStream CheckedOutputStream

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

Introduction

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

Prototype

public CheckedOutputStream(OutputStream out, Checksum cksum) 

Source Link

Document

Creates an output stream with the specified Checksum.

Usage

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  ww .ja  v  a  2s.co 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: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;//from  w  w  w. ja v  a  2s . co  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;//from www.  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:Main.java

public static void compressDir(File file) throws IOException {
    FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
    CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));

    compressDir(file, out, file.getAbsolutePath());

    out.flush();// w ww .  j a  va2s.  c  om
    out.close();
}

From source file:Main.java

public static boolean compress(File file) {
    try {//from w ww  .j a va 2 s. com
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);
        out.closeEntry();

        in.close();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:net.landora.video.info.file.FileHasher.java

public static String getED2KHash(File file) {
    InputStream is = null;//  w w  w  .  j a  v  a 2 s .c  o m
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        Edonkey e2dkChecksum = new Edonkey();

        IOUtils.copy(is, new CheckedOutputStream(new NullOutputStream(), e2dkChecksum));

        return e2dkChecksum.getHexValue();
    } catch (Exception e) {
        LoggerFactory.getLogger(FileHasher.class).error("Error hashing file.", e);
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.littcore.io.util.ZipUtils.java

public static void zip(File srcFileOrPath, File targetFileNamePath) throws IOException {
    //?//w w w . j a  v a  2s  . c  om
    CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
    //zip?
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
    out.setEncoding("GBK");
    zip(out, srcFileOrPath, "");
    out.close();
}

From source file:org.openmrs.module.sync.server.ConnectionRequest.java

/**
 * Public constructor that creates a quest
 * /*from w  ww  .  j a v  a2  s .co  m*/
 * @param content
 * @param useCompression
 * @throws SyncException
 */
public ConnectionRequest(String content, boolean useCompression) throws SyncException {
    try {
        this.useCompression = useCompression;
        this.baos = new ByteArrayOutputStream();
        this.cos = new CheckedOutputStream(baos, new CRC32());

        if (useCompression) {
            this.zos = new GZIPOutputStream(new BufferedOutputStream(cos));
            IOUtils.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), zos);
            IOUtils.closeQuietly(zos);
        } else {
            IOUtils.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), baos);
            IOUtils.closeQuietly(baos);
        }
        this.checksum = cos.getChecksum().getValue();

    } catch (IOException e) {
        throw new SyncException(e);
    }
}

From source file:org.openmrs.module.sync.serialization.ZipPackage.java

public ZipPackage(File root, String target) {
    try {/*from   w ww.  ja va2  s.  c  om*/
        this.rootName = root.getAbsolutePath();
        this.targetName = target;
        File archiveFolder = new File(root, "archive");
        archiveFolder.mkdir();
        File f = new File(archiveFolder, targetName);
        f.mkdir();
        if (!f.exists())
            f.mkdirs();
        File outputStreamDir = new File(f,
                targetName + "_" + SyncConstants.SYNC_FILENAME_MASK.format(new Date()) + ".zip");
        this.dest = new FileOutputStream(outputStreamDir);
        this.checksum = new CheckedOutputStream(dest, new Adler32());
        this.out = new ZipOutputStream(new BufferedOutputStream(checksum));
        this.data = new byte[BUFFER];
    } catch (Exception e) {
        log.error(e.toString());
    }
}

From source file:com.littcore.io.util.ZipUtils.java

/**
 * ?//w  ww .ja v  a  2  s  .  c om
 * @param files 
 * @param targetFileNamePath 
 * @throws IOException
 */
public static void batchZip(File[] files, String targetFileNamePath) throws IOException {
    //?
    CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
    //zip?
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
    File srcFile = null;
    for (int i = 0; i < files.length; i++) {
        srcFile = files[i];
        out.putNextEntry(new ZipEntry(srcFile.getName()));
        FileInputStream in = new FileInputStream(srcFile);
        int len;
        byte[] buf = new byte[BUFFERED_SIZE];
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
}