Bla...
Bla...

Reputation: 7288

Java zip - zip unwanted directory

I'm an OSX user and currently using Play Framework, there is a weird issue when I use java.util.zip to zip some files in a folder. The files will be auto generated (Done) into /Users/vorsz/Desktop/MonthlyReport/August. But when I try to zip them using below code:

val baseURL: String = "/Users/vorsz/Desktop/MonthlyReport/"
val directoryToZip = new File(baseURL+"August").mkdir()
val writer = new PrintWriter(baseURL+"August/test.html", "UTF-8")
writer.println("<Table><tr><td>45296</td><td>2014-07-23</td><td>2014-08-01</td><td>010194</td><td></td><td>120</td><td>C-00322</td><td>ANK 007</td><td></td><td></td></tr></Table>")
writer.close()
val writer1 = new PrintWriter(baseURL+"August/test2.html", "UTF-8")
writer1.println("<Table><tr><td>45296</td><td>2014-07-23</td><td>2014-08-01</td><td>010194</td><td></td><td>120</td><td>C-00322</td><td>ANK 007</td><td></td><td></td></tr></Table>")
writer1.close()

val f = new FileOutputStream(baseURL+"test.zip")
val zip = new ZipOutputStream(new BufferedOutputStream(f))
zip.putNextEntry(new ZipEntry(baseURL+"August/test.html"))
zip.putNextEntry(new ZipEntry(baseURL+"August/test2.html"))
zip.close()

It will produce a zip file in a right folder, however when I extract it, it produces some unwanted folders like Users,vorsz,Desktop and MonthlyReport. So I need to open them up until I can access test.html and test2.html. What I want when I extract the zip file is only an August Folder with its files or the files itself, both way if possible. Can anyone help me here ?

Note: I also had tried implementing ZipUtils class, but still got same result..

Upvotes: 1

Views: 170

Answers (2)

Gagravarr
Gagravarr

Reputation: 48326

Based on your updated code, I think what you need to do instead is not mess about with the file system, and just generate things straight into the zip, along with full control of names

val baseURL: String = "/Users/vorsz/Desktop/MonthlyReport/"

val f = new FileOutputStream(baseURL+"test.zip")
val zip = new ZipOutputStream(new BufferedOutputStream(f))

zip.putNextEntry(new ZipEntry("/August/test.html"))
val writer = new PrintWriter(new OutputStreamWriter(zip, "UTF-8"))
writer.println("<Table><tr><td>45296</td><td>2014-07-23</td><td>2014-08-01</td><td>010194</td><td></td><td>120</td><td>C-00322</td><td>ANK 007</td><td></td><td></td></tr></Table>")
writer.flush()

zip.putNextEntry(new ZipEntry("/August/test2.html"))
val writer1 = new PrintWriter(new OutputStreamWriter(zip, "UTF-8"))
writer1.println("<Table><tr><td>45296</td><td>2014-07-23</td><td>2014-08-01</td><td>010194</td><td></td><td>120</td><td>C-00322</td><td>ANK 007</td><td></td><td></td></tr></Table>")
writer1.flush()

zip.close()

That will add an entry with the name with no base directory, then write the contents in to the zip, then add another entry, then write the other contents in. No file system buffering, no worrying about names, just generating the zip as you go

Upvotes: 2

Ye Win
Ye Win

Reputation: 2098

I think Apache Common Compress library will solve your problem.
I already tested with this library and it work fine.

You can refer not only zip but also many archive type compress and decompress example in below link.

http://commons.apache.org/proper/commons-compress/examples.html

Upvotes: 2

Related Questions