Monday, February 4, 2013

How to work with zip files in Java

Java provides an API to work with compressed file format, zip. java.util.zip is the package that provides classes to compress and decompress files in zip format.

This is how to compress a file to zip file format. If we want to compress myfile.txt to myfilezip.zip into the output folder, first myfile.txt is retrieved via FileInputStream. Data read through FileInputStream writes to ZipOutputStream. ZipEntry provides a way to put file entries to ZipOutputStream by putNextEntry method.



public class ConvertFiletoZip {
       
    public void convertToZip(String file, String outputFolder) throws FileNotFoundException, IOException{
        try{
           
            //Gets myfile.txt as FileInputStream
            File myfile = new File(file);
            FileInputStream fileInputStream = new FileInputStream(myfile);            
           
            FileOutputStream outputStream = new FileOutputStream(outputFolder + "/myfilezip.zip");
            //Creates ZipOutputStream from FileOutputStream
            ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
           
            //Creates a ZipEntry and puts it to ZipOutputStream
            ZipEntry zipEntry = new ZipEntry("myfile");
            zipOutputStream.putNextEntry(zipEntry);        
                       
            byte[] buffer = new byte[(int) myfile.length()];
            int length = 0;
            //Reads bytes from FileInputStream and writes to ZipOutputStream
            while((length = fileInputStream.read(buffer))>0){
                zipOutputStream.write(buffer, 0, length);
            }
           
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            fileInputStream.close();
       
        }catch (IOException e) {
            System.out.println("Error occured when zip file is created");
        }             
    }
   
    public static void main(String[] args) throws FileNotFoundException, IOException{
        ConvertFiletoZip readZipFile = new ConvertFiletoZip();
        readZipFile.convertToZip("/home/kaushalya/myworkspace/ZipManipulation/myfile.txt","/home/kaushalya/myworkspace/ZipManipulation" );
       
    }
}



In the same way, we can read the zip file through  ZipInputStream that is  wrapped in FileInputStream.  getNextEntry() method is used to get the entries of the zip file. The data of the file entry read to a bytes array  is written through FileOutputStream to the output folder.


public class ConvertZiptoFile {

    public static final String ZIP_FILE = "/home/kaushalya/myworkspace/ZipManipulation/files/files.zip";
    public static final String OUTPUT_FOLDER = "/home/kaushalya/myworkspace/ZipManipulation/files";
   
    public void convertToZipToFile(String zipFile, String outputFolder){
        try {
            File folder = new File(OUTPUT_FOLDER);
            if(!folder.exists()){
                folder.mkdir();
            }
           
            FileInputStream fileInputStream = new FileInputStream(zipFile);
            ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
            ZipEntry zipEntry = null;
            while((zipEntry = zipInputStream.getNextEntry()) != null){
                System.out.println("File entry : " + zipEntry.getName());
                File unzippedFile = new File(folder+"/"+zipEntry.getName());
                int length = (int) unzippedFile.length();
                byte[] buffer = new byte[length];
               
                FileOutputStream fileOutputStream = new FileOutputStream(unzippedFile);
                int len =0;
                while((len =zipInputStream.read(buffer))>0){
                    fileOutputStream.write(buffer, 0, len);
                }
               
                zipEntry = null;
            }           
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
    }
   
    public static void main(String[] args){
        ConvertZiptoFile convertZiptoFile = new ConvertZiptoFile();
        convertZiptoFile.convertToZipToFile(ZIP_FILE, OUTPUT_FOLDER);
    }
}

Friday, January 25, 2013

Singleton Design Pattern - Java




There may be situations that we need exactly one instance of a class to perform some functionalities. Singleton design pattern ensures that only one instance of a class is created and provides a global point of access to the object.


Public class MySingleton(){
     private static MySingleton singletonInstance;

     private MySingleton(){
     
     }

     public static MySingleton getSigleton(){
         if(singletonInstance = null)
             singletonInstance = new MySingleton()
         return singletonInstance;
     }
}

If we make the constructor of MySingleton class private, The outside classes can't create instances of MySingleton class. We can create instances only within MySingleton class. Therefore, public static getter method is created to give the instance to outside.


In the situations of using multi threading, getter method has to be put as synchronized because if two threads may access the method at the same time and may create two instances of MySingleton class. But using synchronized keyword may reduce the performance of the program.

Public class MySingleton(){
     private static MySingleton singletonInstance;

     private MySingleton(){
     
     }

     public static synchronized MySingleton getSigleton(){
         if(singletonInstance = null)
             singletonInstance = new MySingletn()
         return singletonInstance;
         }
}

The other option to create a Singleton class is to create the instance when class is loaded. Multi threading problems may not occur because before any of threads access the instance is already created when the class is loaded.

Public class MySingleton(){
     private static final MySingleton singletonInstance = new MySingleton()
     
     private MySingleton(){

     }

     public static MySingleton getMySingletn (){
         return singletonInstance;
     }
}

According to the performance of your program and needs, you can select which method would be better to create the Singleton class. If you are using several class loaders, it may create more than one instance. Singleton class cannot be subclassed because it has a private constructor.


Best way to create a  sigleton class is to use enum as described in Effective Java Reloaded by Joshua Bloch. Enum guarantees only one instance and thread safety. As the below code, we can declare enum Singleton easily. 

public enum MySigleton{

     INSTANCE;

     public void execute(){
         //Other functionalities
     }
}

We can get the singleton instance by using MySingleton.INSTANCE.