Results 1 to 12 of 12
  1. #1
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Writing zip files results in a zip file with a size, but no files when i open it

    I am using ZipOutputStream to write a zip folder, and when I am done copying the zip folder has no files inside. The weird part is that the zip folder has a size of 18kb, which is about right for the files I am writing. My method for writing the zip:
    Java Code:
        public void addFile(File directory, ZipOutputStream out) throws IOException {
            File[] filenames = directory.listFiles();
            byte[] b = new byte[1024];
            for (int i = 0; i < filenames.length; i++) {
              if (filenames[i].isDirectory()) {
                addFile(filenames[i], out);
                continue;
              }
              FileInputStream in = new FileInputStream(filenames[i].getAbsolutePath());
              System.out.println(" Adding: " + filenames[i].getAbsolutePath());
              out.putNextEntry(new ZipEntry(filenames[i].getAbsolutePath()));
              int len;
              while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
              }
              out.closeEntry();
              in.close();
            }
            		Runnable r = new Runnable() {
            			public void run() {
            	      		try {
            	      			Thread.sleep(1000);
            	      			out.flush();
            	      			out.close();
    						System.out.println("closed");
    			        		System.out.println("done");
    						} catch (IOException e) {
    							e.printStackTrace();
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
            			}
            		};
    		Thread thread1 = new Thread(r);
    		thread1.start();
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    What is the Runnable class for? Why don't you close the stream after writing to it?

  3. #3
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    I tried, but then when i put it after the for loop it never closed and whenever i put it after the addFile(..) call it closed to early so i used a Runnable/Thread to make it a bit delayed.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    You should put the close in the same method where you create the ZipOutputStream.

  5. #5
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    Still no files in the zip folder

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    Can you post a small, working program that will show the problem?

  7. #7
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    it produces a zip file that has no contents.
    Java Code:
    package jarmaker;
    
    
    import java.io.*;
    import java.util.zip.*;
    
    public class SimpleZip {
    	public static void main(String[] args) throws IOException {
    		SimpleZip sz = new SimpleZip();
    		File f = new File("C:\\Users\\AJ\\Blah\\");
    		ZipOutputStream out = new ZipOutputStream(new FileOutputStream("C:\\Users\\AJ\\aj.zip"));
    		sz.addFiles(f, out, sz);
    		out.close();
    	}
    	public void addFiles(File directory, ZipOutputStream out, SimpleZip sz) throws IOException {
            File[] filenames = directory.listFiles();
            byte[] b = new byte[1024];
            for (int i = 0; i < filenames.length; i++) {
              if (filenames[i].isDirectory()) {
                addFiles(filenames[i], out, sz);
                continue;
              }
              FileInputStream in = new FileInputStream(filenames[i].getAbsolutePath());
              out.putNextEntry(new ZipEntry(filenames[i].getAbsolutePath()));
              int len;
              while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
              }
              out.closeEntry();
              in.close();
            }
        }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    Your code works for me.
    Are there any exceptions thrown?
    Add a try{}catch block with a call to printStackTrace() to be sure to trap and display any errors.

  9. #9
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    i have figured out part of the problem by opening the zip with 7-Zip. It is creating C:\Users\AJ\Blah\hey.txt inside the zip folder, but i want it to get the files inside the Blah folder and put them in the zip folder, not the whole path.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    not the whole path.
    Your code gets ALL of the files in the folder and sub folders.
    Which files/folders don't you want in the zip file?

    Are you talking about the contents of the ZipEntry that you create for each file?
    Use some of the String classes methods if you want to change the String that goes in the ZipEntry.

  11. #11
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    Oh that was an easy fix. All i changed was getAbsolutePath() to getName()
    Thanks very much!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Writing zip files results in a zip file with a size, but no files when i open it

    You could have name collisions if there are files with the same name in different folders.

Similar Threads

  1. Error with Writing Files, don't know why??
    By teekei in forum New To Java
    Replies: 11
    Last Post: 07-19-2011, 12:26 AM
  2. writing results of recursion into file
    By sara12345 in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 01:22 PM
  3. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  4. Merged TIF file size is more larger than the sum of single TIF files
    By subrahmanyam.adapa in forum Advanced Java
    Replies: 3
    Last Post: 01-26-2009, 01:19 PM
  5. Writing to files within jar files
    By erhart in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 02:50 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
  •