UncategorizedWorking with Zip Files in Java

Working with Zip Files in Java

.zip files – also known as compressed files – are a popular compression file type used across every platform and operating system. Working with .zip files is an essentail aspect of any programming language and especially Java. Here is some sample code showing how to work with .zip files. We will be using the java.util.zip package to list the contents of a .zip file. Here is the sample Java code:

*/

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.nio.file.*;

public class UsingZipFile
{
	public static void main(String args[])
	{
		UsingZipFile usingZipFile = new UsingZipFile();
		usingZipFile.proceed();
	}
	
	private void proceed()
	{
		final String ZIP_FILE_NAME = "CustomZipFile.zip";
		try(ZipFile file = new ZipFile(ZIP_FILE_NAME))
        {
            FileSystem fileSystem = FileSystems.getDefault();
            //Getting list of all the file
            Enumeration<? extends ZipEntry> entries = file.entries();
             
            while (entries.hasMoreElements())
            {
                System.out.println(entries.nextElement());	
			}
		}
        catch(IOException e)
        {
            e.printStackTrace();
        }		
	}
}

/*

Depending upon the files in your zipped folder and the settings of your operating system, you can expect the following output when you run the above code in your integrated development environment (IDE) or code editor:

[[email protected]]# java UsingZipFile
CreateDirectory.class
ListAllFilesInFolder.class
ListOfFilesOFType.class
NetworkIntMulticastSupported.class
RuntimeHalt.class
TimeZonesInJava.class
UnderstandingFileChannels.class
UsingMath.class
UsingStackTraceElement.class
UsingZipFile.class

Latest Posts

Related Stories