aspose file tools*
The moose likes I/O and Streams and the fly likes Looking for better solution for editing a file within a zipped folder? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Looking for better solution for editing a file within a zipped folder?" Watch "Looking for better solution for editing a file within a zipped folder?" New topic
Author

Looking for better solution for editing a file within a zipped folder?

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi All,

I'm working on a project where I need to read about a dozen files within a zipped folder, of which one is a text file, and the rest are image files.
I basically need to editi the text file within the zipped folder and forward the entire zipped folder and contents to another dirrectory/folder location.
Here's the steps I've thought out so far...

1.) I need to save the original zipped folder and its files off incase I need to revert.
So, first I need to create a temporary folder and place a copy of the zipped file there.
In doing this: reading in all the files and writing them out to the temporary folder.

2.) read the text file in the new temporary folder and rewrite it there as well.

3.) read in all the files in the temporary folder and rewrite them as copressed in a zipped folder within the temporary folder.

4.) move the new zipped folder and its contents to the destination path using IO methode "renameTo()".

5.) remove/delete the tempoorary folder.

My question is... Is this the most efficient way to do this using Java?
Please advice...
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
That depends on what you mean by most efficient.

If I was doing this I would make a copy of the zip file (or if you are using Java 7 you can just move the zip file to the temp folder) but not unpack it. I would then read in the files from the archive and write them straight out to a new archive editing the text file as I go.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Unfortunately I'm using version 6 and don't have the authority to upgrade... :-(

"most effiecient" meaning: less code with faster run times, basically better efficiency.

I've already got the the text file read in from the zip folder and have edited the data read-in,
So I'm at a point where I now just need to figure out the best practice to delete the text file
inside the zipped folder in my temporary folder without disrupting the other non-text files in
the same zipped folder and then create a text file in the zipped folder consisting of the updated data.

Any suggestions?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Unfortunately I'm using version 6 and don't have the authority to upgrade... :-(

Not a major problem, it just means you have to copy the zip rather than move it.

"most effiecient" meaning: less code with faster run times, basically better efficiency.

These are mutually exclusive things - less code doesn't necessarily mean more speed. And I know I'm being pedantic here but less code and faster run times than what?
The point is you should not worry about efficiency until you know you have a problem in one area or another and then you should look to identify where in the code the problem is (eg use a profiler), generate some metrics to measure the problem (ie time taken, memory used etc) and only then look at optimizing the code. Without identifying the code that is causing the problem you don't know if you are optimizing the right piece of code and without metrics you don't know if your attempts at optimization are actually working or not.

I've already got the the text file read in from the zip folder and have edited the data read-in, So I'm at a point where I now just need to figure out the best practice to delete the text file inside the zipped folder in my temporary folder without disrupting the other non-text files in the same zipped folder and then create a text file in the zipped folder consisting of the updated data.

You can't update a file in a zip you have to rewrite the whole zip file which is why I said I would read in each file in the zip and write it out to the new zip transforming the text file on the fly.

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi All,

First I wanted to thank you for your comments, they were helpful.

My current issue has changed a bit...
I have been able to read all the files from a zipped folder and wrote them out to a temp folder/directory.
From here I have edited one of the text files and saved it.
Now I need to re-zip the newly edited text file and 9 other pdf files I have in the same temp directory/folder into a single zipped folder.
I've read just about everything I can think of online and in my java books on hand and came up with the following
class to handle this... forgive me if it is not the prettiest code you've seen, it's still a work in progress.
Unfortunately it's not working at this point. which is why I'm here hoping for suggestions.
Here's the code...



What I'm getting at this point is an empty zip file with a size of 653 KB which is
bigger than the original zipped file (the files I'm trying to use to create this new zipped file came from) was.
Also I'm not getting any errors or warnings when I run this, so I think I'm on the right track, but am probrably missing something.

Any inteligent suggestions are welcome.
Thanks in advance!
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
I Figured it out... :-)

It was the following line of code:


Since 'name' was created from the files absolute path name it was nesting folders within the zipped folder causing the zipped file to be corrupted.
I changed the varaible name to contain the short name of the file and all works well in paradise again. :-)

Thanks for being there just incase.
 
Consider Paul's rocket mass heater.
 
subject: Looking for better solution for editing a file within a zipped folder?