Jump to content
  • Advertisement
Sign in to follow this  
grumpyOldDude

Code can't zip xml file to zip folder

This topic is 1032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am trying to create a zip folder and add a file [Content_Types].xml  to the zip folder:-  NewMemo_11202015_140837.zip

My attempts so far have resulted in an invalid zip folder error (not in code, but when i try to open the zip folder), using the code below

 

The original code worked to add folders/.../files at the root to the zip folder, but didn't work for files at the root, hence i had to modify to the code below, but all efforts failed so far. Any help on how i can correct or adjust the code to make it zip the file?

 

Many thanks

   void createZipFolder(){

       Dir4 = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/DCIM/Cideas/SMemo/NewMemo_11202015_140827/";

    	try {
	          zipFolder(  Dir4 + "[Content_Types].xml",     Dir3 + "NewMemo_11202015_140837.zip");
	     } 
             catch (Exception e) {
		  e.printStackTrace();
	     }
        }
    }

    static public void zipFolder( String srcFolder4, String destZipFile) throws Exception {

        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;

        fileWriter = new FileOutputStream(destZipFile);
        zip = new ZipOutputStream(fileWriter);

        addFileToZip2("", srcFolder4, zip);

        zip.flush();
        zip.close();
      }

    static private void addFileToZip2(String path, String srcFile, ZipOutputStream zip)
         throws Exception {

        File folder = new File(srcFile);
        File[] files = folder.listFiles();
        
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(files[0].getParentFile());
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
          zip.write(buf, 0, len);     
        }  
    }    

Share this post


Link to post
Share on other sites
Advertisement
File.getParentFile returns the file's containing the directory. Were there any errors in the program output? I don't think you can open a FileInputStream on a directory. Or were you intending to create an empty directory? According to http://stackoverflow.com/a/740382, you cannot create an empty directory using a ZipStream.

In any case, I don't see any recursion if you were intending to recursively add all files in a directory. A quick search found this promising example: http://examples.javacodegeeks.com/core-java/util/zip/create-zip-file-from-directory-recursively-with-zipoutputstream/

Share this post


Link to post
Share on other sites

Thanks for reply

 

Were there any errors in the program output?

 

There were no errors. But when I try to open zip folder invalid error pops up

 

 

Or were you intending to create an empty directory?

 

No i was trying to add a the xml file to the zip folder, but not zipping the file ifself

 

In any case, I don't see any recursion if you were intending to recursively add all files in a directory.

 

To be honest i'm so new to this that any observations you make is probably right. Having said that the examples I used/modified are from java2s.com and the recursive example i saw was meant to zip all files and subdirectories as well. But the SMemo format doesn't zip all files and subdirectories but only the root folder is a zip file and the rest folder/files added

 

EDIT:   Yipeeeeeeey!!!! SOLVED  

           in case someone needs it, i put my mistake and solution in next post

Edited by alwaysGrey

Share this post


Link to post
Share on other sites

Problem solved,  - apart from my puzzle game - its not a typical gaming problem, but in the unlikely case that someone else should face similar problems 

My mistake was using files[0].getParentFile().  Like factcall22 mentioned. So I changed that to files[0].getAbsolutePath()  and it worked straight. 

 

My reason for using file[0].getparentFile() originally was that a file should be referenced by its parent folder unlike folder which should be absolute. Well it turns out my logic didn't work, and i've wasted hours on this!

 

Update: deflated! not so sure anymore, seems like flawed concept.  

Edited by alwaysGrey

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!