{"id":18,"date":"2005-12-02T19:17:27","date_gmt":"2005-12-02T18:17:27","guid":{"rendered":"http:\/\/relivethefuture.com\/choronzon\/?p=18"},"modified":"2005-12-02T19:17:27","modified_gmt":"2005-12-02T18:17:27","slug":"java-and-zip-files","status":"publish","type":"post","link":"https:\/\/relivethefuture.com\/choronzon\/java-and-zip-files\/","title":{"rendered":"Java and zip files"},"content":{"rendered":"<p>I&#8217;ve just spent a day debugging what at first appeared to be a permissions problem. Im using the jakarta commons file upload with a tomcat \/ spring setup to allow users to upload zip files, which are then unpacked and the contents are processed into a database. Obviously using a zip should make it easier to upload multiple files and directory structures.<\/p>\n<p>What was happening is that the directory entries I expected to be in the zip werent being created, so none of the files could be written when it was unpacked. Eventually i discovered that the problem was related to how different tools create zip files. I use winrar, which creates a seperate entry for a directory, as luck would have it the tool my client was using doesnt.<\/p>\n<p>So, if you are writing java code to unpack zip files you will be better off doing something like this :<\/p>\n<pre>\nEnumeration entries = zip.entries();\n\n    while (entries.hasMoreElements())\n    {\n        ZipEntry zipEntry = (ZipEntry) entries.nextElement();\n        File file = new File(outputDirectory, zipEntry.getName());\n\n        \/\/ check the parent of this entry to see if it exists\n        if(!file.getParentFile().exists())\n        {\n            file.getParentFile().mkdir();\n        }\n\n\tInputStream is = zip.getInputStream(zipEntry);\n        FileOutputStream fos = new FileOutputStream(file);\n\n\twhile (is.available() > 0)\n        {\n            fos.write(is.read());\n        }\n\n\tfos.close();\n\tis.close();\n}\n<\/pre>\n<p>rather than just checking if zipEntry.isDirectory() and then doing a mkdir.<\/p>\n<p>Of course its wise to do both, because the zip may contain empty directories and you might rely on them being present later on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just spent a day debugging what at first appeared to be a permissions problem. Im using the jakarta commons file upload with a tomcat \/ spring setup to allow users to upload zip files, which are then unpacked and the contents are processed into a database. Obviously using a zip should make it easier &hellip; <a href=\"https:\/\/relivethefuture.com\/choronzon\/java-and-zip-files\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java and zip files<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[49,50,86,91,99],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-development","category-java","tag-jakarta-commons","tag-java","tag-spring","tag-tomcat","tag-zip-files"],"_links":{"self":[{"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/relivethefuture.com\/choronzon\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}