Thursday, 24 November 2016

Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Directory)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=XjXMP1zUWQ0&list=UUhwKlOVR041tngjerWxVccw

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

public class ZipDemo
{
    private ArrayList<String> fileList;
    private static final String OUTPUT_ZIP_FILE = "D:/work/Java.zip";
    private static final String SOURCE_FOLDER = "D:/work/java";

    public ZipDemo()
    {
        fileList = new ArrayList<String>();
    }

    public static void main(String[] args) throws IOException
    {
        ZipDemo zipDemo = new ZipDemo();
        zipDemo.generateFileList(new File(SOURCE_FOLDER));
        zipDemo.zipIt(OUTPUT_ZIP_FILE);
    }

    public void zipIt(String zipFile) throws IOException
    {

        byte[] buffer = new byte[1024];

        /*
         * If the Streams are within the
         * "try-With-Resources" block, then it will be
         * closed automatically.
         */
        try (
                FileOutputStream fos = new FileOutputStream(zipFile);
                ZipOutputStream zos = new ZipOutputStream(fos))
        {

            System.out.println("Output to Zip : " + zipFile);

            for (String file : this.fileList)
            {

                System.out.println("File Added : " + file);
                ZipEntry ze = new ZipEntry(file);
                zos.putNextEntry(ze);

                FileInputStream in = new FileInputStream(SOURCE_FOLDER
                        + File.separator + file);

                int len;
                while ((len = in.read(buffer)) > 0)
                {
                    zos.write(buffer, 0, len);
                }

                in.close();
            }

            zos.closeEntry();

            System.out.println("Zip file is created...");
        }

    }

    /**
     * Traverse a directory and get all files, and add the
     * file into fileList.
     */
    public void generateFileList(File node)
    {

        // add file only
        if (node.isFile())
        {
            fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
        }

        if (node.isDirectory())
        {
            String[] subNote = node.list();
            for (String filename : subNote)
            {
                generateFileList(new File(node, filename));
            }
        }

    }

    /**
     * Format the file path for zip
     */
    private String generateZipEntry(String file)
    {
        return file.substring(SOURCE_FOLDER.length() + 1, file.length());
    }
}
Output
Output to Zip : D:/work/Java.zip
File Added : CityInfoDemo\.classpath
File Added : CityInfoDemo\.project
File Added : CityInfoDemo\.settings\.jsdtscope
---
---
File Added : CityInfoDemo\WebContent\WEB-INF\web.xml
Zip file is created...
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_zip_dir_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_zip_dir_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/97750dcb5a5d126b3956b792a4fc850ca4dd67ca/BasicJava/JavaIODemo_zip_dir_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment