java 文件crc_Java ZipEntry setCrc()用法及代码示例

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

函数抛出错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值