Results 1 to 4 of 4
  1. #1
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Unzipping a file

    I can't see what I am doing wrong here, maybe someone else can see it.

    I keep getting this error:

    run:
    Enter the source directory and zipfile name:
    C:\Chpt4Disc4.zip
    Extracting: Chpt4Disc4/test_chpt4_disc4.docx
    java.io.FileNotFoundException: Chpt4Disc4\test_chpt4_disc4.docx (The system cannot find the file specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
    at chapter4discovery4.Unzip.main(Unzip.java:29)

    BUILD SUCCESSFUL (total time: 20 seconds)

    Here is my code:

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    
    public class Unzip {
    
        static final int BUFFER = 2048;
    
        public static void main(String args[]) throws IOException {
    
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the source directory and zipfile name: ");
            String input = in.readLine();
    
            try {
                BufferedOutputStream os = null;
                BufferedInputStream is = null;
                ZipEntry entry;
                ZipFile zipfile = new ZipFile(input);
                Enumeration e = zipfile.entries();
                while (e.hasMoreElements()) {
                    entry = (ZipEntry) e.nextElement();
                    System.out.println("Extracting: " + entry);
                    is = new BufferedInputStream(zipfile.getInputStream(entry));
                    int count;
                    byte data[] = new byte[BUFFER];
                    FileOutputStream fos = new FileOutputStream(entry.getName());
                    os = new BufferedOutputStream(fos, BUFFER);
                    while ((count = is.read(data, 0, BUFFER))
                            != -1) {
                        os.write(data, 0, count);
                    }
                    os.flush();
                    os.close();
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    The biggest room in the world, is room for improvement.

  2. #2
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Ok... if I create a folder with the same name as my folder in my zip-file, it will
    extract the file fine, but how can I get it to create this folder when I run it,
    rather than creating the folder first to put the file in.
    The biggest room in the world, is room for improvement.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried running the program on a different zip file, perhaps a small simpler one? Perhaps the one you're using is corrupted, or password protected, or in some other way messed up.

    Edit: never mind...

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    To create a folder using java:
    Quote Originally Posted by sdileepk View Post
    File f = new File("C:\TEST");
    try{
    if(f.mkDir())
    System.out.println("Directory Created");
    else
    System.out.println("Directory is not created");
    }catch(Exception e){
    e.printStacktrace();
    }
    In your case, you might have to mess around with the zip file abs. path to get it's dir.

Similar Threads

  1. Replies: 8
    Last Post: 02-18-2010, 01:54 PM
  2. unzipping core files failed error 25099
    By truckerej in forum New To Java
    Replies: 17
    Last Post: 08-20-2009, 05:25 AM
  3. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  4. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  5. Unzipping
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •