Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to zip the contents inside a directory

 
Mohnish Parmar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a zip file of all contents inside a directory including subfolders & files,
I have a directory called Documents, in my code Document directory gets included in zip file, but I want only the contents within the Document directory
i.e folder1,folder2,folder3, file.txt
it should exclude Document directory in zip file, but all the contents(folders & files) within it, should be present
Please Help.

The directory structure is:

Document/
>folder1 ( Document/folder1 )
>file1.txt ( Document/folder1/file1.txt )
folder2 ( Document/folder2 )
>file2.txt ( Document/folder2/file2.txt )
folder3 ( Document/folder3 )
>file3.txt ( Document/folder3/file3.txt )
file.txt ( Document/file.txt )

Code:


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Document directory is being included because you are adding it to listFiles on line 19. Instead you should be adding all the files and directories in Documents at this point.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Mohnish Parmar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to add only the contents within directory in line 16 if possible
or is the way to remove "Document/" or "Document"in addFolderToZip method
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would get rid of your compressFiles method and replace lines 17 - 21 with
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic