Java ZipEntry setCrc() function with examples


The setCrc() function is a part of java.util.zip package. The function is used to set the CRC value of the specified Zip Entry. The CRC is an error detecting code used to detect error in raw data.
Function Signature :

public void setCrc(long val)

Syntax :

zip_entry.setCrc(val);

Parameters :The function takes a long value, which represents the CRC value
Return value :The function does not return any value.
Exceptions :The function throws IllegalArgumentException if the specified CRC-32 value is less than 0 or greater than 0xFFFFFFFF



Below programs illustrates the use of setCrc() function

Example 1: We will create a file named zip_file and get the zip file entry using setEntry() function and then set the CRC-32 of the specified ZipEntry.”file.zip” is a zip file present in f: directory. We will take a “.zip” file as ZipEntry

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

Crc : 190

Example 2: We will create a file named zip_file and get the zip file entry using getEntry() function and then set the CRC-32 of the specified ZipEntry.”file.zip” is a zip file present in f: directory. We will set the value of CRC to -1. We will take a “.cpp” file as ZipEntry

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

invalid entry crc-32

The function throws an error

Reference:https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html#setCrc-long-



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.