setCrc()函数是java.util.zip软件包的一部分。该功能用于设置指定的邮政编码条目的CRC值。 CRC是用于检测原始数据中的错误的错误检测代码。函数签名:
public void setCrc(long val)
用法:
zip_entry.setCrc(val);
参数:该函数取一个long值,代表CRC值
返回值:该函数不返回任何值。
Exceptions:如果指定的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
函数抛出错误