DEV Community

Pramod Bablad
Pramod Bablad

Posted on

How To ZIP File/Directory In Java?

How To Zip A Single File In Java?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFileJavaProgram
{
private static void zipSingleFile(String filePath) throws IOException
{
//Step 1 : Create a File fileToBeZipped with given filePath
//and wrap it into FileInputStream fis

    File fileToBeZipped = new File(filePath);
    FileInputStream fis = new FileInputStream(fileToBeZipped);

    //Step 2 : Create FileOutputStream fos with path to output file 
    //and wrap it into ZipOutputStream zos

    FileOutputStream fos = new FileOutputStream("I:\\ZippedFile.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);

    //Step 3 : Create zipEntry with the name of fileToBeZipped

    ZipEntry zipEntry = new ZipEntry(fileToBeZipped.getName());

    //Step 4 : Read fis byte by byte into byte array bytes and 
    //write them into zos till the end of fis

    zos.putNextEntry(zipEntry); 

    byte[] bytes = new byte[1024];

    while (fis.read(bytes) >= 0)
    {
        zos.write(bytes, 0, bytes.length);
    }

    zos.closeEntry();

    //Step 5 : Close the resources

    fis.close();
    zos.close();
    fos.close();
}

public static void main(String[] args) throws IOException
{
    String filePath = "I:\\TextFileOne.txt";

    zipSingleFile(filePath);
}
Enter fullscreen mode Exit fullscreen mode

}

How To Zip Multiple Files In Java?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFileJavaProgram
{
private static void zipMultipleFiles(String... filePaths) throws IOException
{
//Step 1 : Creating FileOutputStream fos and ZipOutputStream zos

    FileOutputStream fos = new FileOutputStream("I:\\ZippedFile.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);

    //Step 2 : Creating a List of filePaths

    List<String> filesToBeZipped = Arrays.asList(filePaths);

    FileInputStream fis;

    //Step 3 : Zipping each file in filesToBeZipped one by one

    for (String filePath : filesToBeZipped) 
    {
        File fileToBeZipped = new File(filePath);
        fis = new FileInputStream(fileToBeZipped);

        ZipEntry zipEntry = new ZipEntry(fileToBeZipped.getName());

        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];

        while (fis.read(bytes) >= 0)
        {
            zos.write(bytes, 0, bytes.length);
        }

        zos.closeEntry();
        fis.close();
    }

    //Step 4 : Close the resources

    zos.close();
    fos.close();
}

public static void main(String[] args) throws IOException
{
    String[] filePaths = new String[] {"I:\\TextFileOne.txt", "I:\\TextFileTwo.txt", "I:\\TextFileThree.txt"} ;

    zipMultipleFiles(filePaths);
}
Enter fullscreen mode Exit fullscreen mode

}

How To Zip A Whole Directory In Java?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFileJavaProgram
{
//Method to ZIP single file

private static void zipSingleFile(String filePath, ZipOutputStream zos) throws IOException
{
    File fileToBeZipped = new File(filePath);
    FileInputStream fis = new FileInputStream(fileToBeZipped);

    ZipEntry zipEntry = new ZipEntry(filePath);

    zos.putNextEntry(zipEntry); 

    byte[] bytes = new byte[1024];

    while (fis.read(bytes) >= 0)
    {
        zos.write(bytes, 0, bytes.length);
    }

    zos.closeEntry();

    fis.close();
}

//Method To ZIP Whole directory

private static void zipWholeDirectory(String filePath, ZipOutputStream zos) throws IOException
{
    File fileToBeZipped = new File(filePath);

    if(fileToBeZipped.isDirectory())
    {
        File[] files = fileToBeZipped.listFiles();

        for (File file : files) 
        {
            zipWholeDirectory(filePath+"\\"+file.getName(), zos);
        }           
    }
    else
    {
        zipSingleFile(filePath, zos);
    }
}

public static void main(String[] args) throws IOException
{
    //Step 1 : Let filePath be the path of the directory to be zipped

    String filePath = "I:\\Java\\jdk1.8.0_181\\src";

    //Step 2 : Creating FileOutputStream and ZipOutputStream

    FileOutputStream fos = new FileOutputStream("I:\\ZippedFile.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);

    //Step 3 : Call to zipWholeDirectory() 

    zipWholeDirectory(filePath, zos);

    //Step 4 : Close the resources

    zos.close();
    fos.close();
}
Enter fullscreen mode Exit fullscreen mode

}

Source : https://javaconceptoftheday.com/how-to-zip-file-directory-in-java/

Top comments (0)