File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes  Problem When Compressing Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Problem When Compressing Directory" Watch " Problem When Compressing Directory" New topic
Author

Problem When Compressing Directory

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi,

I am using the following code for compressing the content of the given folder--



Now suppose the folder to be zipped is -- c:\\ziptest\\zipdir

the output comes in this for way -- zipdir.zip and wen i unzip this the structure comes like c:\\ziptest\\zipdir, same as given dir.

Now what my need is that wen i unzip the zipdir.zip it just have the file , no directory structure.

Can you please help me out?

Thanks,
Kaya.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

I haven't play with ZipXStream and stuff but think how you would zip dirs/files using WinZip directly.

If you right-click on the directory and click winzip - the whole directory gets added to the zip file. And this is apparently what you got so far. So I think you now need to get inside the directory level then add those files.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

You just need to play around with the ZipEntry.

Change this from:

to:


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Too difficult a question for beginners. Moving.
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Thanks a lot Freddy Wong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

A technique I often* use when zipping a folder recursively is the following:
This recursive call will build a full path for the ZIP entry names; for example "root/folder/subfolder/file.ext". That way, the full directory structure will remain intact in the ZIP file.


* Well actually once. I've simply put the code in a common utility class


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sadanand munswamy
Ranch Hand

Joined: Apr 10, 2012
Posts: 39
Hi Rob,
Many thanks, for your prompt reply. But i am still not getting it worked as per my need,
Have been trying to tweak the API for a long time now. Can you please be a little more elaborate.
sadanand munswamy
Ranch Hand

Joined: Apr 10, 2012
Posts: 39
Hi Rancher,
http://www.mkyong.com/java/how-to-compress-files-in-zip-format/comment-page-1/#comment-81940
this works perfectly as needed.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

sadanand munswamy wrote:Hi Rob,
Many thanks, for your prompt reply. But i am still not getting it worked as per my need,
Have been trying to tweak the API for a long time now. Can you please be a little more elaborate.

If I take your example of zipping folder C:\dir1\dir2\dir3 from this thread.

Assume that inside dir3 there are files file1 and file2, and sub directory dir4 with files file3 and file4.
I will then start by calling addToZip with a File object for C:\dir1\dir2\dir3 and a name of "".
The method will see that the file argument is a directory. It will then iterate over its sub folders and files (let's call these f). This will cause method calls addToZip(stream, f, "file1"), addToZip(stream, f, "file2") and addToZip(stream, f, "dir4").
The first two method calls will end because the file argument will not be a directory.
The third one will see that the file argument is a directory, and iterate over its contents again. It will now call addToZip(stream, f, "dir4/file3") and addToZip(stream, f, "dir4/file4").

As a result, the ZIP file will contain the following files:
- file1
- file2
- dir4/file3
- dir4/file4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem When Compressing Directory