UncategorizedWorking with Zip Files in Java

Working with Zip Files in Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

.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:

[root@mypc]# java UsingZipFile
CreateDirectory.class
ListAllFilesInFolder.class
ListOfFilesOFType.class
NetworkIntMulticastSupported.class
RuntimeHalt.class
TimeZonesInJava.class
UnderstandingFileChannels.class
UsingMath.class
UsingStackTraceElement.class
UsingZipFile.class

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories