当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ZipEntry getCrc()用法及代码示例



getCrc()函数是java.util.zip软件包的一部分。该函数返回特定ZipEntry的CRC-32校验和值。 CRC是用于检测原始数据中的错误的错误检测代码。函数签名:

public long getCrc()

用法:

zip_entry.getCrc();

参数:此函数不需要任何参数。
返回值:该函数返回一个long值,即ZipEntry的Crc。
Exceptions:该函数不会引发任何异常。0


以下示例程序旨在说明getCrc()函数的使用

范例1:我们将创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目,然后获取指定ZipEntry的CRC-32。“ file.zip”是f:目录中的一个zip文件。我们将以“.zip”文件作为ZipEntry

// Java program to demonstrate the 
// use of getCrc() 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"); 
  
            // get the Zip Entry using 
            // the getEntry() function 
            ZipEntry entry = zip_file.getEntry("file.zip"); 
  
            // 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:1798120178

范例2:我们将创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目,然后获取指定ZipEntry的CRC-32。“ file.zip”是f:目录中的一个zip文件。我们将以“.cpp”文件作为ZipEntry

// Java program to demonstrate the 
// use of getCrc() 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"); 
  
            // get the Zip Entry using 
            // the getEntry() function 
            ZipEntry entry = zip_file.getEntry("file1.cpp"); 
  
            // 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:3528251335

参考: https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html#getCrc-



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 Java ZipEntry getCrc() function with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。