Example usage for java.util.zip CheckedOutputStream getChecksum

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

Introduction

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

Prototype

public Checksum getChecksum() 

Source Link

Document

Returns the Checksum for this output stream.

Usage

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. j  a va  2 s.  c o 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   w  w w  .jav a 2 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 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  2 s  .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:org.apache.hadoop.mapred.TestShuffleHandler.java

private static void createIndexFile(File indexFile, Configuration conf) throws IOException {
    if (indexFile.exists()) {
        System.out.println("Deleting existing file");
        indexFile.delete();//w w  w .j  a  v  a2 s  .  c  om
    }
    indexFile.createNewFile();
    FSDataOutputStream output = FileSystem.getLocal(conf).getRaw()
            .append(new Path(indexFile.getAbsolutePath()));
    Checksum crc = new PureJavaCrc32();
    crc.reset();
    CheckedOutputStream chk = new CheckedOutputStream(output, crc);
    String msg = "Writing new index file. This file will be used only " + "for the testing.";
    chk.write(Arrays.copyOf(msg.getBytes(), MapTask.MAP_OUTPUT_INDEX_RECORD_LENGTH));
    output.writeLong(chk.getChecksum().getValue());
    output.close();
}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void zipFiles(File archive, File inputDir) throws IOException {

    FileOutputStream dest = new FileOutputStream(archive);
    CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));

    zipEntries(out, inputDir, inputDir);

    out.close();/* w  w w.j a v a  2  s  .  co m*/
    log.debug("checksum:" + checksum.getChecksum().getValue());

}

From source file:io.milton.cloud.server.apps.calendar.CalendarManager.java

private void updateCtag(CalEvent event) {
    OutputStream nulOut = new NullOutputStream();
    CheckedOutputStream cout = new CheckedOutputStream(nulOut, new Adler32());
    appendLine(event.getDescription(), cout);
    appendLine(event.getSummary(), cout);
    appendLine(event.getTimezone(), cout);
    appendLine(event.getStartDate(), cout);
    appendLine(event.getEndDate(), cout);
    Checksum check = cout.getChecksum();
    long crc = check.getValue();
    event.setCtag(crc);/*from   w w  w. j  a  va  2  s .  c  om*/
    updateCtag(event.getCalendar());
}

From source file:org.spliffy.server.apps.calendar.CalendarManager.java

private void updateCtag(CalEvent event) {
    OutputStream nulOut = new NullOutputStream();
    CheckedOutputStream cout = new CheckedOutputStream(nulOut, new Adler32());
    HashUtils.appendLine(event.getDescription(), cout);
    HashUtils.appendLine(event.getSummary(), cout);
    HashUtils.appendLine(event.getTimezone(), cout);
    HashUtils.appendLine(event.getStartDate(), cout);
    HashUtils.appendLine(event.getEndDate(), cout);
    Checksum check = cout.getChecksum();
    long crc = check.getValue();
    event.setCtag(crc);/*from w w  w .j a  v  a  2s .  c  o  m*/
    updateCtag(event.getCalendar());
}

From source file:org.spliffy.server.apps.calendar.CalendarManager.java

private void updateCtag(Calendar sourceCal) {
    OutputStream nulOut = new NullOutputStream();
    CheckedOutputStream cout = new CheckedOutputStream(nulOut, new Adler32());

    HashUtils.appendLine(sourceCal.getColor(), cout);
    if (sourceCal.getEvents() != null) {
        for (CalEvent r : sourceCal.getEvents()) {
            String name = r.getName();
            String line = HashUtils.toHashableText(name, r.getCtag(), "");
            HashUtils.appendLine(line, cout);
        }//from w  w w  .  ja  va 2 s  .c o  m
    }
    Checksum check = cout.getChecksum();
    long crc = check.getValue();
    sourceCal.setCtag(crc);
}

From source file:io.milton.cloud.server.apps.calendar.CalendarManager.java

private void updateCtag(Calendar sourceCal) {
    OutputStream nulOut = new NullOutputStream();
    CheckedOutputStream cout = new CheckedOutputStream(nulOut, new Adler32());

    appendLine(sourceCal.getColor(), cout);
    if (sourceCal.getEvents() != null) {
        for (CalEvent r : sourceCal.getEvents()) {
            String name = r.getName();
            String line = HashCalc.getInstance().toHashableText(name, r.getCtag() + "", "");
            appendLine(line, cout);/*from w w w  .  j  av a  2 s  . c  o m*/
        }
    }
    Checksum check = cout.getChecksum();
    long crc = check.getValue();
    sourceCal.setCtag(crc);
}

From source file:org.akvo.flow.service.DataSyncService.java

private ZipFileData formZip(long surveyInstanceId) {
    ZipFileData zipFileData = new ZipFileData();
    StringBuilder surveyBuf = new StringBuilder();

    // Hold the responses in the StringBuilder
    String uuid = processSurveyData(surveyInstanceId, surveyBuf, zipFileData.imagePaths);

    // THe filename will match the Survey Instance UUID
    File zipFile = new File(FileUtil.getFilesDir(FileType.DATA), uuid + ConstantUtil.ARCHIVE_SUFFIX);

    // Write the data into the zip file
    try {//from   w ww .  j ava2  s.  c  o  m
        String fileName = zipFile.getAbsolutePath();// Will normalize filename.
        zipFileData.filename = fileName;
        Log.i(TAG, "Creating zip file: " + fileName);
        FileOutputStream fout = new FileOutputStream(zipFile);
        CheckedOutputStream checkedOutStream = new CheckedOutputStream(fout, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(checkedOutStream);

        writeTextToZip(zos, surveyBuf.toString(), SURVEY_DATA_FILE);
        String signingKeyString = mProps.getProperty(SIGNING_KEY_PROP);
        if (!StringUtil.isNullOrEmpty(signingKeyString)) {
            MessageDigest sha1Digest = MessageDigest.getInstance("SHA1");
            byte[] digest = sha1Digest.digest(surveyBuf.toString().getBytes("UTF-8"));
            SecretKeySpec signingKey = new SecretKeySpec(signingKeyString.getBytes("UTF-8"), SIGNING_ALGORITHM);
            Mac mac = Mac.getInstance(SIGNING_ALGORITHM);
            mac.init(signingKey);
            byte[] hmac = mac.doFinal(digest);
            String encodedHmac = Base64.encodeBytes(hmac);
            writeTextToZip(zos, encodedHmac, SIG_FILE_NAME);
        }

        final String checksum = "" + checkedOutStream.getChecksum().getValue();
        zos.close();
        Log.i(TAG, "Closed zip output stream for file: " + fileName + ". Checksum: " + checksum);
    } catch (IOException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    } catch (NoSuchAlgorithmException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    } catch (InvalidKeyException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    }

    return zipFileData;
}