Results 1 to 4 of 4

Thread: Create Zip File in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    67

    Create Zip File in Java

    Hi, I want to create a zip file in java. Can it be possible to create a zip file in java? If yes then please tell me how to create this zip file with the help of java. I have basic knowledge of java, so if possible then provide me the program which will able to create zip file.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Create Zip File in Java

    Hi, I dont think that there will be any possibility to create a jar file. I know that you can create a jar file in java. Basically jar file is created in java for making all the classes in one single file as well as to compress the whole files to run that particular project or code. It is similar to the creation of the exe file you can say. It loads directly when you double click on it, if you have java virtual machine in your computer.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Create Zip File in Java

    While creating zip files from java the java.util.zip package is used for creating Zip files. The example below creates a Zip file from a normal text file. I think by looking at the code you can get idea about the logic behind the code. It is not that much complicated code. So, it don't need any type of explanation.
    Code:
    import java.io.*;
    import java.util.zip.*;
    
    class  zipfile
    {
        public static void main(String[] args) 
    {
            if (args.length != 2) 
    {
                System.out.println("Usage: java zipfile[files to be zipped] [filename after zip] ");
                return;
            }
    try 
    {
                String fname = args[0];
                String zipfname = args[1];
                zipfilelist = new zipfile( );
                list.doZip(fname,zipfname);
            } 
    catch (Exception e) 
    {
                e.printStackTrace();
            }
        }
        
        public void doZip(String fname,String zipfname) 
    {
            try
     {
                byte[] buf = new byte[1024];
                FileInputStream fileis = new FileInputStream(fname);
                fileis.read(buf,0,buf.length);            
                CRC32 crc = new CRC32();
                ZipOutputStream zoutstream = new ZipOutputStream((OutputStream)new FileOutputStream(zipfname));
                zoutstream.setLevel(6);            
                ZipEntry entry = new ZipEntry(fname);
                entry.setSize((long)buf.length);
                crc.reset();
                crc.update(buf);
                entry.setCrc( crc.getValue());
                zoutstream.putNextEntry(entry);
                zoutstream.write(buf, 0, buf.length);
                zoutstream.finish();
                zoutstream.close();
            } 
    
    catch (Exception e) 
    {
                e.printStackTrace();
    }
    }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Create Zip File in Java

    Hi, to create a zip file in java you need to use the following code. I don't know how does this code works. But I have got this on internet. I think this will work for you. Use it and then try to run it.

    Code:
    import java.io.*;
    import java.util.zip.*;
    
    public class CreateZip
    {
    	public static void main(String[] args) throws IOException
    	{
    		System.out.println(" ZIP file creation.");
    		System.out.println("Please enter file name which you want to zip : ");
    		BufferedReader bfreader = new BufferedReader(new InputStreamReader(System.in));
    		String filename = bfreader.readLine();
    		File f = new File(filename);
    		if(!f.exists())
    		{
    			System.out.println("File not found.");
    			System.exit(0);
    		}
    		System.out.print("Please enter zip file name with extension .zip : ");
    		String zipFileName = bfreader.readLine();
    		byte[] buffer = new byte[18024];
    		try
    		{
    			ZipOutputStream outputname = new ZipOutputStream(new FileOutputStream(zipFileName));
    			outputname.setLevel(Deflater.DEFAULT_COMPRESSION);
    			FileInputStream in = new FileInputStream(filename);
    			outputname.putNextEntry(new ZipEntry(filename));
    			int len;
    			while ((len = in.read(buffer)) > 0)
    			{
    				outputname.write(buffer, 0, len);
    			}
    			outputname.closeEntry();
    			in.close();
    			outputname.close();
    		}
    		catch (IllegalArgumentException e)
    		{
    			e.printStackTrace();
    			System.exit(0);
    		}
    		catch (FileNotFoundException e)
    		{
    			e.printStackTrace();
    			System.exit(0);
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    			System.exit(0);
    		}
    	}
    }

Similar Threads

  1. Replies: 1
    Last Post: 21-03-2013, 03:52 PM
  2. Windows 7: Unable to create file with File system error (65535)
    By TheHibiscus in forum Operating Systems
    Replies: 4
    Last Post: 23-01-2011, 07:07 PM
  3. Can I create .exe file in java?
    By LostIt in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 06:23 PM
  4. How to create XML file in Java
    By Suzane in forum Software Development
    Replies: 3
    Last Post: 27-04-2009, 06:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,690,692.67983 seconds with 16 queries