Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem When Compressing Directory

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3440
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to play around with the ZipEntry.

Change this from:

to:
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for beginners. Moving.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Freddy Wong.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sadanand munswamy
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic