ZIP API in Java
Last Updated :
25 Oct, 2021
Zip files are compressed files that is used for transferring files over the internet. Zip and unzip are very important operations while writing application programs. Java provides a rich API to zip and unzip the files. By using ZIP API in java we can zip and unzip into standards ZIP and GZIP formats.
As always we will be discussing constructors than moving onto methods furthermore implementing the ZIp API. Let us start off with the constructors as follows:
1. Default constructor: Zip entry with the specified name gets created and it takes string name as an argument.
ZipEntry() {}
2. Parameterized constructor: New zip entry gets created by using the specified file
2.1 ZipEntry e
ZipEntry(e) {}
2.2 File file: For opening and reading ZIP files for a specified file object
ZipFile(File) {}
2.3 For opening and reading ZIP files for a specified file object
ZipFile(File file, Charset charset) {{}
Now let us do discuss methods which are as follows:
Method 1: getEntry(): Tells zip file entry for the specified name, or null if not found.
Syntax:
getEntry()
Return type: ZipEntry
Parameters: String name
Method 2: getInputStream(): Used to get the input stream for reading the contents of specified zip entries.
Syntax:
getNextEntry()
Return type : InputStream
parameters: ZipEntry zipEntry
Method 3: putNextentry(): Starts writing a new ZIP file entry. Then it positions the stream to the start of entry data.
Syntax:
putNextentry() {}
Return Type: Void
Parameters: ZipEntry e
Implementation:
Example 1
Java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class GFG {
public static void main(String[] argv)
{
GfgZipDemo.zip_one_file();
}
public static void zip_one_file()
{
byte [] buffer = new byte [ 1024 ];
try {
FileOutputStream fos = new FileOutputStream(
"DESTINATION_PATH_WHERE_YOU_WANT_YOUR_ZIP" );
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry( "testing1.txt" );
zos.putNextEntry(ze);
FileInputStream in
= new FileInputStream( "FILE_TO_ZIP_PATH" );
int lengths;
while ((lengths = in.read(buffer)) > 0 ) {
zos.write(buffer, 0 , lengths);
}
in.close();
zos.closeEntry();
zos.close();
System.out.println( "Successfully compiled and executed." );
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
|
Output: On console generated after entering your path you will get the following output
Successfully compiled and executed.
It can be perceived from the image where the file is generated as shown in the below media as follows:
Example 2
Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class GFG {
List fileList;
private static final String INPUT_ZIP_FILE
= "YOUR_ZIP_FILE_PATH" ;
private static final String OUTPUT_FOLDER
= "YOUR_UNZIPPED_ZIP_FILE_PATH" ;
public static void main(String[] args)
{
GFG unZip = new GFG();
unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
}
public void unZipIt(String zipFile, String outputFolder)
{
byte [] buffer = new byte [ 1024 ];
try {
File folder = new File(OUTPUT_FOLDER);
if (!folder.exists()) {
folder.mkdir();
}
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while (ze != null ) {
String fileName = ze.getName();
File newFile
= new File(outputFolder + File.separator
+ fileName);
System.out.println(
"file unzip : "
+ newFile.getAbsoluteFile());
new File(newFile.getParent()).mkdirs();
FileOutputStream fos
= new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0 ) {
fos.write(buffer, 0 , len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println(
"Successfully compiled and executed." );
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
|
Output: On console generated after entering your path you will get the following output
Successfully compiled and executed.
It can be perceived from the image where the file as shown in below media as follows:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...