Results 1 to 13 of 13
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Read Contents of Zip File W/O Extracting

    I am attempting to utilize the syntax found here
    How to read contents of a ZIP file in Java

    but I keep getting an error of java.util.zip.ZipException: error in opening zip file

    and full syntax is
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            chooser = new JFileChooser(); 
            chooser.setCurrentDirectory(new java.io.File("."));
            if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {  
                File selected = chooser.getSelectedFile();
                String fullName = selected.getAbsolutePath();
                read(fullName);
    		}
    	}
    	    private static void read(String zipFilePath) {
            try {
                ZipFile zipFile = new ZipFile(zipFilePath);
     
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
     
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    String name = entry.getName(); 
                    System.out.println(name);
                }
     
                zipFile.close();
            } catch (IOException ex) {
                System.err.println(ex);
            }
        }
    What do I need to change so the contents of the .zip can be output w/o extracting the zip?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,966
    Rep Power
    7

    Default Re: Read Contents of Zip File W/O Extracting

    Are you sure the zip file is valid? Do you have an SSCCE or MCVE?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Read Contents of Zip File W/O Extracting

    Quote Originally Posted by SurfMan View Post
    Are you sure the zip file is valid? Do you have an SSCCE or MCVE?
    It should be a valid .zip. Regardless here is the file with full source code contained inside as I zipped my Netbeans folder.
    Attached Files Attached Files

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Read Contents of Zip File W/O Extracting

    Is the attached zip file the one that caused the exception?
    Have you looked at the zip file that caused the exception with any zip file utilities?
    What happened?

    I tried the code in post#1 with a jar file and it worked for me.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Read Contents of Zip File W/O Extracting

    Quote Originally Posted by Norm View Post
    Is the attached zip file the one that caused the exception?
    Have you looked at the zip file that caused the exception with any zip file utilities?
    What happened?

    I tried the code in post#1 with a jar file and it worked for me.
    Yes - the .zip file attached in my previous post is the one that throws the error. I have tried a .zip created by both winzip (the one attached) and created by 7Zip a .7z extension and I get the same error both times.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Read Contents of Zip File W/O Extracting

    The JavaApplication1.zip file is not a zip file. Try Renaming it to have an extension of .7z and use the 7Zip program.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Read Contents of Zip File W/O Extracting

    Quote Originally Posted by Norm View Post
    The JavaApplication1.zip file is not a zip file. Try Renaming it to have an extension of .7z and use the 7Zip program.
    I get this error - java.util.zip.ZipException: error in opening zip file using the attached newly created .7z file

    okay - when it is in a .7z extension the site tells me invalid format.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Read Contents of Zip File W/O Extracting

    error in opening zip file using the attached newly created .7z file
    Yes, that is correct. The file is not in zip format.

    I renamed it to .7z and opened it with the 7-Zip program with no problems.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,966
    Rep Power
    7

    Default Re: Read Contents of Zip File W/O Extracting

    This is a 7zip file. If you open the the file in a notepad program, you can see the first bytes have a 7z value. You can't go renaming a file, hoping that the content will change accordingly. If it's a 7z file, it stays a 7z file, no matter what you rename it to. The fact that it opens in Winzip and 7Zip is that these programs are smart enough to examine the header of the file instead of relying on the extension.

    A Word document that you rename to XLSX doesn't automagically turn into a spreadsheet, so why whould a 7z file be otherwise?

    Read Contents of Zip File W/O Extracting-capture.png
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Read Contents of Zip File W/O Extracting

    So if I am following then the syntax I posted above will work for a .zip file, but not for a .7z file?

    Is there a way to achieve the same result with a .7z file in Java?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Read Contents of Zip File W/O Extracting

    way to achieve the same result with a .7z file in Java?
    Find a library of classes and methods written in java that read 7-Zip files
    or write your own.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Read Contents of Zip File W/O Extracting

    Quote Originally Posted by Norm View Post
    Find a library of classes and methods written in java that read 7-Zip files
    or write your own.
    Very interesting. In my mind one .zip is all the same, but I learn they are not.

    I am desiring to design a Java GUI that can read contents of any type of .zip, .7z, .gz etc file w/o extracting the file. Obviously, the code in my OP will work for a .zip, but is there any tool, or resource, that can handle all types?

    Or will I need to use a case statement to get which type of .zip file it is, then process with the java depending on the file type?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Read Contents of Zip File W/O Extracting

    is there any tool, or resource, that can handle all types?
    Probably not.
    You will have to do some research, learn the contents of each type and write some code to read the file and extract the necessary data.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Read file and displayed contents on windows
    By Rmcgrath in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2013, 09:50 PM
  2. How to read the contents of uploaded file?
    By HardRock in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-25-2012, 11:07 AM
  3. how to read a file and change its contents.
    By mointariq in forum New To Java
    Replies: 1
    Last Post: 04-30-2012, 07:19 PM
  4. Read and Write depending on file contents
    By paulhenderson in forum Java Applets
    Replies: 3
    Last Post: 02-15-2010, 12:38 PM
  5. Replies: 2
    Last Post: 05-11-2009, 10:07 AM

Posting Permissions

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