Monday, 28 November 2016

Java Tutorial : Java IO (Java File - Compressing and Decompressing File - Deflater)


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

Click the below Image to Enlarge
Java Tutorial : Java IO (Java File - Compressing and Decompressing File - Deflater) 
Java Tutorial : Java IO (Java File - Compressing and Decompressing File - Deflater) 
Java Tutorial : Java IO (Java File - Compressing and Decompressing File - Deflater) 
Java Tutorial : Java IO (Java File - Compressing and Decompressing File - Deflater) 
CompressFileDemo.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;

class CompressFileDemo
{
    public static void main(String args[]) throws IOException
    {
        CompressFileDemo compressFileDemo = new CompressFileDemo();
        compressFileDemo.compressFile("D:/work/HelloWorld.java",
                                            "D:/work/Compressed.txt");
    }

    private void compressFile(String fileToCompress, String compressFile)
            throws IOException
    {
        try (
                FileInputStream fin = new FileInputStream(fileToCompress);
                FileOutputStream fout = new FileOutputStream(compressFile);
                DeflaterOutputStream dos = new DeflaterOutputStream(fout);)
        {

            int i;
            while ((i = fin.read()) != -1)
            {
                dos.write((byte) i);
                dos.flush();
            }

        }

        System.out.println("Compression is done..");
    }
}
Output
Compression is done..
DecompressFileDemo.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;

public class DecompressFileDemo
{

    public static void main(String[] args) throws IOException
    {
        DecompressFileDemo decompressFileDemo = new DecompressFileDemo();
        decompressFileDemo.decompressFile("D:/work/Compressed.txt",
                                            "D:/work/HelloWorld.java");

    }

    private void decompressFile(String fileToDeCompress, String deCompressFile)
            throws IOException
    {
        try (
                FileInputStream fin = new FileInputStream(fileToDeCompress);
                InflaterInputStream in = new InflaterInputStream(fin);
                FileOutputStream fout = new FileOutputStream(deCompressFile);)
        {

            int i;
            while ((i = in.read()) != -1)
            {
                fout.write((byte) i);
                fout.flush();
            }

        }

        System.out.println("DeCompression is done..");

    }

}
Output
DeCompression is done..

Refer: 
https://en.wikipedia.org/wiki/DEFLATE

https://docs.oracle.com/javase/8/docs/api/index.html?java/util/zip/DeflaterOutputStream.html


Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_deflate_compress_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5edb9b935a4ef60d2a672d03f038390cd08d4eb7/BasicJava/JavaIODemo_deflate_compress_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

    Google+ Followers

    Google+ Badge