Zipp Code

Discussion in 'Java' started by sallu, Jan 19, 2005.

  1. sallu

    sallu New Member

    Joined:
    Oct 21, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Java program for zipping.Download the file given as attachment.
    unzip the file and store it in any folder in ur hard-drive....

    go to command prompt....

    set the classpath properly.
    C:\Zipper\[if the files are stored in this folder, then set the classpath in the following manner]
    C:\Zipper>set classpath=.

    then type
    C:\Zipper>java Zipper
    Do u want to make zip file(Y/N)?
    y
    Do u want to add to archive(Y/N)?
    y
    Give the file name with path:E:\a

    Do u want to add to archive(Y/N)?
    y

    Give the file name with path:E:\b.txt

    Do u want to add to archive(Y/N)?
    n

    Enter the location where the zip file will be located:
    E:\

    Enter the zip file name:///Don't give any extension
    testZip

    Creating Zip file....Wait..........
    Zip file created....

    Also read the readme file carefully.

    Please Rate..........................
    Code:
    import java.io.*;
    import java.util.zip.*;
    import java.util.*;
    class Zipper
    {
    	public static void main(String[] args)
    	{
    		Vector v=new Vector(1,1);
    		String str;
    		String zipLocation="";
    		String zipName="";
    		int i=0;
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    		try
    		{
    			System.out.println("Do u want to make zip file(Y/N)?");
    			str=br.readLine();
    			if(str.equalsIgnoreCase("Y"))
    				while(true)
    				{
    					System.out.println("Do u want to add to archive(Y/N)?");
    					str=br.readLine();
    					if(str.equalsIgnoreCase("Y"))
    					{
    						System.out.print("Give the file name with path:");
    						v.add(br.readLine());
    						System.out.println();
    
    					}
    					else if(str.equalsIgnoreCase("N"))
    					{
    						System.out.println("Enter the location where the zip file will be located:");
    						zipLocation=br.readLine();
    						System.out.println("Enter the zip file name:");
    						zipName=br.readLine();
    						if(zipLocation.length()==0)
    						{
    							System.out.println("The zip file location has not been enterred...");
    							throw new Exception();
    						}
    						else if(zipName.length()==0)
    						{
    							System.out.println("The zip file location has not been enterred...");
    							throw new Exception();
    						}
    						break;
    					}
    					else
    					{
    						System.out.println("The input parameter is not recognized.");
    						continue;
    					}
    				}
    			else if(str.equalsIgnoreCase("N"))
    				System.exit(0);
    			else
    			{
    				System.out.println("Invalid input parameter.Exiting from program.......");
    				System.exit(0);
    			}
    
    			File[] listToBeZipped=new File[v.size()];
    			for(i=0;i<v.size();i++)
    				listToBeZipped[i]=new File((v.get(i)).toString());
    
    			FileOutputStream fout=new FileOutputStream(zipLocation+"\\"+zipName+".zip");
    			CheckedOutputStream csum=new CheckedOutputStream(fout,new Adler32());
    			ZipOutputStream zos=new ZipOutputStream(csum);
    			//BufferedOutputStream bwr=new BufferedOutputStream(zos);
    			System.out.println("Creating Zip file....Wait..........");
    			ZippingUtility obZipUtil=new ZippingUtility(listToBeZipped,zipLocation,zipName,zos);
    			zos.close();
    			System.out.println("Zip file created....");
    		}
    		catch(ZipException e)
    		{
    			System.out.println("No file selected for zipping");
    		}
    		catch(Exception e)
    		{
    			System.out.println("Exception occurred in main()."+e);
    			e.printStackTrace();
    		}
    
    	}
    }
    
    class ZippingUtility
    {
    	private	File[] listToBeZipped;
    	private String zipLocation;
    	private String zipName;
    	//private BufferedOutputStream bwr;
    	private ZipOutputStream zos;
    	byte[] readBuffer = new byte[2156];
    	int bytesIn = 0;
    
    	ZippingUtility(File[] listToBeZipped,String zipLocation,String zipName,ZipOutputStream zos)
    	{
    		this.listToBeZipped=new File[listToBeZipped.length];
    		for(int i=0;i<listToBeZipped.length;i++)
    			this.listToBeZipped[i]=listToBeZipped[i];
            this.zipLocation=zipLocation;
    		this.zipName=zipName;
    		this.zos=zos;
    		//this.bwr=new BufferedOutputStream(zos);
    		//this.bwr=bwr;
    		ZipBegin();
    	}
    
    	void ZipBegin()
    	{
    		try
    		{
    
    
    			for(int i=0;i<listToBeZipped.length;i++)
    			{
                    if(!listToBeZipped[i].exists())
    				{
    					System.out.println("The file "+	listToBeZipped[i]+" does not exist.");
    					continue;
    				}
    				if(listToBeZipped[i].isDirectory())
    				{
    					File[] list=listToBeZipped[i].listFiles();
    					new ZippingUtility(list,zipLocation,zipName,zos);
    				}
                    else
                    {
    					FileInputStream fis = new FileInputStream(listToBeZipped[i]);
    					//create a new zip entry
    					ZipEntry anEntry = new ZipEntry(listToBeZipped[i].getPath());
    					//place the zip entry in the ZipOutputStream object
    					zos.putNextEntry(anEntry);
    					//now write the content of the file to the ZipOutputStream
    					while((bytesIn = fis.read(readBuffer)) != -1)
    					{
    						zos.write(readBuffer, 0, bytesIn);
    					}
    					//close the Stream
    		   			fis.close();
    					/*FileReader fr=new FileReader(f);
    					BufferedReader br=new BufferedReader(fr);
    					int c;
    					ZipEntry zen=new ZipEntry(f.getPath());
    					zos.putNextEntry(zen);
    					while((c=br.read())!=-1)
    						bwr.write(c);
    					br.close();*/
    				}
                }
            }
    		catch(FileNotFoundException e)
    		{
    			System.out.println("The files you entered does not exist.");
    			System.exit(1);
    		}
    		catch(Exception e)
    		{
    			System.out.println("Exception occurred..");
    			e.printStackTrace();
    		}
    	}
    
    }
     

    Attached Files:

    Last edited: Jan 19, 2005
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hey Sallu You are adding nice little utilities for the users of Go4Expert. I really like your codes and snippets.
     
  3. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    http://www.go4expert.com
    good work ... it is amazing how java provides packages for everything we can think of .. java.util.zip makes zippppping so easy without worrying about the details of the actual compression algo ..

    Cheers,
    Amit Ray.
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Hey really nice code snippet and helped to some extent in my work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice