HomeLanguagesJavaZipFile size() function in Java with examples

ZipFile size() function in Java with examples

The size() function is a part of java.util.zip package. The function returns the number of entries of the zip file.

Function Signature:

public int size()

Syntax:

zip_file.size();

Parameters: The function does not require any parameters
Return value: The function returns a Integer, which is the number of entries of the zip file
Exceptions: The function throws IllegalStateException if the zip file has been closed

Below programs illustrates the use of size() function
Example 1: Create a file named zip_file and get the number of entries using size() function.”file.zip” is a zip file present in f: directory.




// Java program to demonstrate the
// use of size() function
  
import java.util.zip.*;
  
public class solution {
    public static void main(String args[])
    {
        try {
  
            // Create a Zip File
            ZipFile zip_file
                = new ZipFile("f:\\file.zip");
  
            // Display the number of entries
            // of the zip file
            // using size() function
            System.out.println("number of entries = "
                               + zip_file.size());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Output:

number of entries= 7

Example 2: Create a file named zip_file and get the number of entries using size() function. we will try to see whether the function throws an exception if we close the file and then call the function size().




// Java program to demonstrate the
// use of size() function
  
import java.util.zip.*;
  
public class solution {
    public static void main(String args[])
    {
  
        try {
  
            // Create a Zip File
            ZipFile zip_file
                = new ZipFile("f:\\file.zip");
  
            // close the file
            zip_file.close();
  
            // Display the number of entries
            // of the zip file
            // using size() function
            System.out.println("number of entries = "
                               + zip_file.size());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Output:

zip file closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html#size()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS