Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem unzipping larger files with Java

807588May 25 2009 — edited May 26 2009
When I extract small zip files with java it works fine. If I extract large zip files I get errors. Can anyone help me out please?
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.zip.*;

public class  updategrabtest
{
	public static String filename = "";
	//public static String filesave = "";
    public static boolean DLtest = false, DBtest = false;
	
	// update
	public static void main(String[] args)
	{
		System.out.println("Downloading small zip");
		download("small.zip"); // a few k
		System.out.println("Extracting small zip");
		extract("small.zip");
		
		System.out.println("Downloading large zip");
		download("large.zip"); // 1 meg
		System.out.println("Extracting large zip");
		extract("large.zip");
		System.out.println("Finished.");
		
		// update database
		boolean maindb = false; //database wasnt updated
	}
	
	// download
	public static void download (String filesave)
	{

		try
		{
			java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
			java.net.URL("http://saveourmacs.com/update/" + filesave).openStream());
			java.io.FileOutputStream fos = new java.io.FileOutputStream(filesave);
			java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
			
			byte data[] = new byte[1024];
			
			while(in.read(data,0,1024)>=0)
			{
				bout.write(data);
			}
			bout.close();
			in.close();
		}
		catch (Exception e)
		{
			System.out.println ("Error writing to file");
			//System.exit(-1);
		}

	}
	
	// extract
	public static void extract(String filez) 
    {
		filename = filez;
		
        try
        {
            updategrab list = new updategrab( );
            list.getZipFiles();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

	// extract (part 2)
    public static void getZipFiles()
    {
        try
        {
            //String destinationname = ".\\temp\\";
            String destinationname = ".\\";
            byte[] buf = new byte[1024]; //1k
            ZipInputStream zipinputstream = null;
            ZipEntry zipentry;
            zipinputstream = new ZipInputStream(
                new FileInputStream(filename));

            zipentry = zipinputstream.getNextEntry();
            
			while (zipentry != null) 
            { 
                //for each entry to be extracted
                String entryName = zipentry.getName();
                System.out.println("entryname "+entryName);
                int n;
                FileOutputStream fileoutputstream;
                File newFile = new File(entryName);
                String directory = newFile.getParent();
                
                if(directory == null)
                {
                    if(newFile.isDirectory())
                        break;
                }
                
                fileoutputstream = new FileOutputStream(
                   destinationname+entryName);             

                while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
                    fileoutputstream.write(buf, 0, n);

                fileoutputstream.close(); 
                zipinputstream.closeEntry();
                zipentry = zipinputstream.getNextEntry();

            }//while

            zipinputstream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2009
Added on May 25 2009
9 comments
1,168 views