當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.zip.Adler32.getValue()用法及代碼示例


描述

這個java.util.zip.Adler32.getValue()方法返回校驗和值。

聲明

以下是聲明java.util.zip.Adler32.getValue()方法。

public long getValue()

返回

當前校驗和值。

先決條件

在裏麵創建一個文件 Hello.txtD:> test > 包含以下內容的目錄。

This is an example.

示例

下麵的例子展示了 java.util.zip.Adler32.getValue() 方法的用法。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Adler32Demo {

   private static String SOURCE_FILE = "D:\\test\\Hello.txt";
   private static String TARGET_FILE = "D:\\test\\Hello.zip";

   public static void main(String[] args) {
      byte[] buffer = new byte[1024];

      try {
         FileOutputStream fout = new FileOutputStream(TARGET_FILE);
         CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32());
         ZipOutputStream zout = new ZipOutputStream(checksum);

         FileInputStream fin = new FileInputStream(SOURCE_FILE);
         zout.putNextEntry(new ZipEntry(SOURCE_FILE));
         int length;
         while((length = fin.read(buffer)) > 0) {
            zout.write(buffer, 0, length);
         }

         zout.closeEntry();
         fin.close();
         zout.close();
         System.out.println("Zip file generated!");
         System.out.println("Adler32 Checksum is:" + checksum.getChecksum().getValue());
      } catch(IOException ioe) {
         System.out.println("IOException:" + ioe);
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Zip file generated!
Adler32 Checksum is:504112553

相關用法


注:本文由純淨天空篩選整理自 java.util.zip.Adler32.getValue() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。