集册 Java实例教程 使用CRC32校验和创建Zip文件

使用CRC32校验和创建Zip文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

673
使用CRC32校验和创建Zip文件


import java.io.FileInputStream;

import java.io.FileOutputStream;//时   代     Java  公  众  号 - nowjava.com 提 供

import java.io.IOException;

import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

 

public class Main {

       

        public static void main(String args[])

        {              

                String zipFile = "C:/Folder/zipdemo.zip";

                String sourceFile = "C:/Folder/sourcefile.doc";

               

                byte[] buffer = new byte[1024];

               

                try
                /** from 
                N o w J a v a . c o m - 时  代  Java**/

                {

                        //create object of FileOutputStream

                        FileOutputStream fout = new FileOutputStream(zipFile);

                        CheckedOutputStream checksum = new CheckedOutputStream(fout, new CRC32());

                        //create an object of ZipOutputStream

                        ZipOutputStream zout = new ZipOutputStream(checksum);

                        FileInputStream fin = new FileInputStream(sourceFile);

                        zout.putNextEntry(new ZipEntry(sourceFile));

                        int length;

                        while((length = fin.read(buffer)) > 0)

                        {

                           zout.write(buffer, 0, length);

                        }

                        zout.closeEntry();

                        fin.close();

                        zout.close();

                        System.out.printl
展开阅读全文