Java ZipEntry setCrc()函数及示例
setCrc()函数是Java.util.zip 包的一部分。该函数用于设置指定Zip Entry的CRC值。 CRC 是用于检测原始数据中的错误的错误检测代码。
函数签名:
public void setCrc(long val)
句法 :
zip_entry.setCrc(val);
参数:函数取long值,表示CRC值
返回值:该函数不返回任何值。
异常:如果指定的 CRC-32 值小于 0 或大于 0xFFFFFFFF,则函数抛出IllegalArgumentException
下面的程序说明了 setCrc()函数的使用
示例 1:我们将创建一个名为 zip_file 的文件,并使用 setEntry()函数获取 zip 文件条目,然后设置指定 ZipEntry 的 CRC-32。“file.zip”是存在于 f: 目录中的 zip 文件。我们将使用“.zip”文件作为 ZipEntry
// Java program to demonstrate the
// use of setCrc() function
import java.util.zip.*;
import java.util.Enumeration;
import java.util.*;
import java.io.*;
public class solution {
public static void main(String args[])
{
try {
// Create a Zip File
ZipFile zip_file = new ZipFile("f:\\file1.zip");
// set the Zip Entry using
// the setEntry() function
ZipEntry entry = zip_file.getEntry("file.zip");
// set the Crc
// using the setCrc()
// function
entry.setCrc(190);
// get the Crc
// using the getCrc()
// function
long input = entry.getCrc();
// Display the Crc
System.out.println("Crc : " + input);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
Crc : 190
示例 2:我们将创建一个名为 zip_file 的文件,并使用 getEntry()函数获取 zip 文件条目,然后设置指定 ZipEntry 的 CRC-32。“file.zip”是存在于 f: 目录中的 zip 文件。我们将 CRC 的值设置为 -1。我们将“.cpp”文件作为 ZipEntry
// Java program to demonstrate the
// use of setCrc() function
import java.util.zip.*;
import java.util.Enumeration;
import java.util.*;
import java.io.*;
public class solution {
public static void main(String args[])
{
try {
// Create a Zip File
ZipFile zip_file = new ZipFile("f:\\file1.zip");
// set the Zip Entry using
// the setEntry() function
ZipEntry entry = zip_file.getEntry("file1.cpp");
// set the Crc
// using the setCrc()
// function
entry.setCrc(-1);
// get the Crc
// using the getCrc()
// function
long input = entry.getCrc();
// Display the Crc
System.out.println("Crc : " + input);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
invalid entry crc-32
函数抛出错误
参考: https: Java/util/zip/ZipEntry.html#setCrc-long-