• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem When Compressing Directory

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to play around with the ZipEntry.

Change this from:

to:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for beginners. Moving.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Freddy Wong.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic