1

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..

Community
  • 1
  • 1
Bla...
  • 7,228
  • 7
  • 27
  • 46
  • Why are you passing in the `baseURL` prefix to the ZipEntries? That gets embedded in the name, which then ends up in the zip name. – Gagravarr Dec 16 '14 at 05:15
  • Then how can I point it to `August` directory ? – Bla... Dec 16 '14 at 05:23
  • I can't see the code where you add the file's contents into the zip stream, but normally you'd give the full file path to the bit which adds the data, and the short path to the bit where you record the name – Gagravarr Dec 16 '14 at 05:32
  • I just add file generation code.. What do you mean by "bit"? This is my first time using this library, so it would be great if you can give clear example.. – Bla... Dec 16 '14 at 05:36
  • I haven't tried your code, but my hunch is that you'll be getting 0 byte files in your zip, as I can't see any code that adds the contents of the file to the zip, only the file entries. Are you sure you're getting their contents in the zip? What does the output of `zip -l` on the resulting zip show? – Gagravarr Dec 16 '14 at 05:43
  • I already unzip/extract the zipped file and I can get the files, however it's "encapsulated" into `Users >> vorsz >> Desktop >> MonthlyReport >> August >> files`.. What I want when I extract the file is August >> files or files.. – Bla... Dec 16 '14 at 05:47

2 Answers2

2

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

Ye Win
  • 2,020
  • 14
  • 21
  • just add this apache lib to your Play Framework.. it's not ok? – Ye Win Dec 16 '14 at 05:48
  • Well, Play Framework use something like maven repository.. So, I need some kind of link to get the library.. However, my issue is not on the archive type.. It's more about the structure of generated zip files. – Bla... Dec 16 '14 at 05:50
  • oh I see.I just mentioned you to more powerful zip lib if you want to use for save time. – Ye Win Dec 16 '14 at 05:55
  • Just FYI: I test your code with in just java project. It will work fine and I never seen unwanted directory. But I changed Desktop folder path to partition column as baseURL = "E:/MonthlyReport/"; – Ye Win Dec 16 '14 at 06:32
  • Try to put the files into nested directories.. Like "E:/Users/vorsz/Desktop/MonthlyReport", see if you can still remove the unwanted directory.. – Bla... Dec 16 '14 at 06:55
  • Hmm, that's weird.. When I extract it I got Users, vorsz, Desktop and MonthlyReport directories also. Thanks though for spending your time, @Gagravarr solution works.. here is an upvote.. ^^ – Bla... Dec 16 '14 at 07:57
2

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

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Have you tested it ? I got the following error: `overloaded method constructor PrintWriter`.. – Bla... Dec 16 '14 at 06:56
  • Try now - I'd put the charset on the wrong object when converting your code, sorry – Gagravarr Dec 16 '14 at 07:29
  • Sweet, this even better since I don't need to create the file itself.. Thanks bro.. I'll try to complete this feature now.. – Bla... Dec 16 '14 at 07:58