{"id":583,"date":"2015-01-28T21:25:22","date_gmt":"2015-01-29T06:25:22","guid":{"rendered":"http:\/\/blog.box.kr\/?p=583"},"modified":"2015-01-28T21:25:22","modified_gmt":"2015-01-29T06:25:22","slug":"java%eb%a1%9c-unzip","status":"publish","type":"post","link":"https:\/\/blog.box.kr\/?p=583","title":{"rendered":"java\ub85c unzip"},"content":{"rendered":"<p><a href=\"http:\/\/sourceforge.net\/projects\/jazzlib\/\">http:\/\/sourceforge.net\/projects\/jazzlib\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Java comes with \u201c<strong>java.util.zip<\/strong>\u201d library to perform data compression in ZIp format. The overall concept is quite straightforward.<\/p>\n<ol>\n<li>Read file with \u201c<strong>FileInputStream<\/strong>\u201d<\/li>\n<li>Add the file name to \u201c<strong>ZipEntry<\/strong>\u201d and output it to \u201c<strong>ZipOutputStream<\/strong>\u201c<\/li>\n<\/ol>\n<h4>1. Simple ZIP example<\/h4>\n<p>Read a file \u201c<strong>C:\\spy.log<\/strong>\u201d and compress it into a zip file \u2013 \u201c<strong>C:\\MyFile.zip<\/strong>\u201c.<\/p>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"java\">package com.mkyong.zip;\n\u00a0\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.util.zip.ZipEntry;\nimport java.util.zip.ZipOutputStream;\n\u00a0\npublic class App\n{\n    public static void main( String[] args )\n    {\n    \tbyte[] buffer = new byte[1024];\n\u00a0\n    \ttry{\n\u00a0\n    \t\tFileOutputStream fos = new FileOutputStream(\"C:\\MyFile.zip\");\n    \t\tZipOutputStream zos = new ZipOutputStream(fos);\n    \t\tZipEntry ze= new ZipEntry(\"spy.log\");\n    \t\tzos.putNextEntry(ze);\n    \t\tFileInputStream in = new FileInputStream(\"C:\\spy.log\");\n\u00a0\n    \t\tint len;\n    \t\twhile ((len = in.read(buffer)) &gt; 0) {\n    \t\t\tzos.write(buffer, 0, len);\n    \t\t}\n\u00a0\n    \t\tin.close();\n    \t\tzos.closeEntry();\n\u00a0\n    \t\t\/\/remember close it\n    \t\tzos.close();\n\u00a0\n    \t\tSystem.out.println(\"Done\");\n\u00a0\n    \t}catch(IOException ex){\n    \t   ex.printStackTrace();\n    \t}\n    }\n}<\/pre>\n<\/div>\n<\/div>\n<div>\n<p>2. Advance ZIP example \u2013 Recursively\n<\/p><\/div>\n<p>Read all files from folder \u201c<strong>C:\\testzip<\/strong>\u201d and compress it into a zip file \u2013 \u201c<strong>C:\\MyFile.zip<\/strong>\u201c. It will recursively zip a directory as well.<\/p>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"java\">package com.mkyong.zip;\n\u00a0\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.zip.ZipEntry;\nimport java.util.zip.ZipOutputStream;\n\u00a0\npublic class AppZip\n{\n    List&lt;String&gt; fileList;\n    private static final String OUTPUT_ZIP_FILE = \"C:\\MyFile.zip\";\n    private static final String SOURCE_FOLDER = \"C:\\testzip\";\n\u00a0\n    AppZip(){\n\tfileList = new ArrayList&lt;String&gt;();\n    }\n\u00a0\n    public static void main( String[] args )\n    {\n    \tAppZip appZip = new AppZip();\n    \tappZip.generateFileList(new File(SOURCE_FOLDER));\n    \tappZip.zipIt(OUTPUT_ZIP_FILE);\n    }\n\u00a0\n    \/**\n     * Zip it\n     * @param zipFile output ZIP file location\n     *\/\n    public void zipIt(String zipFile){\n\u00a0\n     byte[] buffer = new byte[1024];\n\u00a0\n     try{\n\u00a0\n    \tFileOutputStream fos = new FileOutputStream(zipFile);\n    \tZipOutputStream zos = new ZipOutputStream(fos);\n\u00a0\n    \tSystem.out.println(\"Output to Zip : \" + zipFile);\n\u00a0\n    \tfor(String file : this.fileList){\n\u00a0\n    \t\tSystem.out.println(\"File Added : \" + file);\n    \t\tZipEntry ze= new ZipEntry(file);\n        \tzos.putNextEntry(ze);\n\u00a0\n        \tFileInputStream in =\n                       new FileInputStream(SOURCE_FOLDER + File.separator + file);\n\u00a0\n        \tint len;\n        \twhile ((len = in.read(buffer)) &gt; 0) {\n        \t\tzos.write(buffer, 0, len);\n        \t}\n\u00a0\n        \tin.close();\n    \t}\n\u00a0\n    \tzos.closeEntry();\n    \t\/\/remember close it\n    \tzos.close();\n\u00a0\n    \tSystem.out.println(\"Done\");\n    }catch(IOException ex){\n       ex.printStackTrace();\n    }\n   }\n\u00a0\n    \/**\n     * Traverse a directory and get all files,\n     * and add the file into fileList\n     * @param node file or directory\n     *\/\n    public void generateFileList(File node){\n\u00a0\n    \t\/\/add file only\n\tif(node.isFile()){\n\t\tfileList.add(generateZipEntry(node.getAbsoluteFile().toString()));\n\t}\n\u00a0\n\tif(node.isDirectory()){\n\t\tString[] subNote = node.list();\n\t\tfor(String filename : subNote){\n\t\t\tgenerateFileList(new File(node, filename));\n\t\t}\n\t}\n\u00a0\n    }\n\u00a0\n    \/**\n     * Format the file path for zip\n     * @param file file path\n     * @return Formatted file path\n     *\/\n    private String generateZipEntry(String file){\n    \treturn file.substring(SOURCE_FOLDER.length()+1, file.length());\n    }\n}<\/pre>\n<\/div>\n<\/div>\n<p><em>Output<\/em><\/p>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"bash\">Output to Zip : C:MyFile.zip\nFile Added : pdfJava-Interview.pdf\nFile Added : spylogspy.log\nFile Added : utf-encoded.txt\nFile Added : utf.txt\nDone<\/pre>\n<\/div>\n<\/div>\n<div class=\"note\"><strong>Follow Up<\/strong><br \/>\nYou may interest at this \u2013 <a href=\"http:\/\/www.mkyong.com\/java\/how-to-decompress-files-from-a-zip-file\/\">How to decompress it from a Zip file<\/a>\n<\/div>\n<h4>Reference<\/h4>\n<ol>\n<li><a href=\"http:\/\/java.sun.com\/developer\/technicalArticles\/Programming\/compression\/\" target=\"_blank\">Compressing and Decompressing Data Using Java APIs<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/sourceforge.net\/projects\/jazzlib\/ &nbsp; Java comes with \u201cjava.util.zip\u201d library to perform data compression in ZIp format. The overall concept is quite straightforward. Read file with \u201cFileInputStream\u201d Add the file name to \u201cZipEntry\u201d and output it to \u201cZipOutputStream\u201c 1. Simple ZIP example Read a file \u201cC:\\spy.log\u201d and compress it into a zip file \u2013 \u201cC:\\MyFile.zip\u201c. package com.mkyong.zip; \u00a0 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; \u00a0 public class App { public static void main( String[] args ) { byte[] buffer = new byte[1024]; \u00a0 try{ \u00a0 FileOutputStream fos = new FileOutputStream(&#8220;C:\\MyFile.zip&#8221;); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze= new ZipEntry(&#8220;spy.log&#8221;); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(&#8220;C:\\spy.log&#8221;); \u00a0 int len; while ((len = in.read(buffer)) &gt; 0) { zos.write(buffer, 0, len); } \u00a0 in.close(); zos.closeEntry(); \u00a0 \/\/remember close it zos.close(); \u00a0 System.out.println(&#8220;Done&#8221;); \u00a0 }catch(IOException ex){ ex.printStackTrace(); } } } 2. Advance ZIP example \u2013 Recursively Read all files from folder \u201cC:\\testzip\u201d and compress it into a zip file \u2013 \u201cC:\\MyFile.zip\u201c. It will recursively zip a directory as well. package com.mkyong.zip; \u00a0 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; \u00a0 public class AppZip { List&lt;String&gt; fileList; private static final String OUTPUT_ZIP_FILE = &#8220;C:\\MyFile.zip&#8221;; private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"ngg_post_thumbnail":0,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[15,7],"tags":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5q9Zn-9p","jetpack-related-posts":[{"id":156,"url":"https:\/\/blog.box.kr\/?p=156","url_meta":{"origin":583,"position":0},"title":"reading text file with utf-8 encoding using java","date":"2014-07-06","format":false,"excerpt":"CASE 1. PrintStream out =newPrintStream(System.out,true,\"UTF-8\"); out.println(str); CASE 2. import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;publicclass test {publicstaticvoid main(String[] args){try{File fileDir =newFile(\"PATH_TO_FILE\");BufferedReader in =newBufferedReader(newInputStreamReader(newFileInputStream(fileDir),\"UTF8\"));String str;while((str = in.readLine())!=null){System.out.println(str);} in.close();}catch(UnsupportedEncodingException e){System.out.println(e.getMessage());}catch(IOException e){System.out.println(e.getMessage());}catch(Exception e){System.out.println(e.getMessage());}} }","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":625,"url":"https:\/\/blog.box.kr\/?p=625","url_meta":{"origin":583,"position":1},"title":"[\ud38c]JAVA \ud654\uc77c \uac10\uc2dc \ud504\ub85c\uadf8\ub7a8 \ub9cc\ub4e4\uae30.","date":"2015-03-21","format":false,"excerpt":"\uc77c\uc744 \ud558\ub2e4\ubcf4\uba74 \ud2b9\uc815 \ub514\ub809\ud1a0\ub9ac\ub97c \uac10\uc2dc\ud558\uc5ec \ud2b9\uc815 \uc870\uac74\uc774 \ub418\uba74 \uc874\uc7ac \ud558\ub294 \ud654\uc77c\uc744 \ucc98\ub9ac \ud558\ub294 \ud504\ub85c\uadf8\ub7a8\uc774 \ud544\uc694 \ud560\ub54c\uac00 \uc788\ub2e4. \u00a0 \ubcf4\ud1b5\uc740 C\/C++\ub85c \uc791\uc5c5 \ud558\ub294\ub370 \uc5b4\ucc0c\ub2e4 \ubcf4\ub2c8 \ubcf4\uac8c\ub41c \ube14\ub85c\uadf8\uc5d0\uc11c \uc5b4\ub5a4 \ubd84\uc774 JAVA\ub85c \ub9cc\ub4e4\uc5b4 \ub193\uc740\uac8c \uc788\uc5b4\uc11c \uac08\ubb34\ub9ac \ud55c\ub2e4. \u00a0 \u00a0 http:\/\/okky.kr\/article\/272376 \u00a0 Main.java public class Main { \/\/ file moved original public static final\u2026","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":448,"url":"https:\/\/blog.box.kr\/?p=448","url_meta":{"origin":583,"position":2},"title":"[\ud38c]Java Mail \ubc1c\uc1a1 (GMail SMTP \uc774\uc6a9) example \uc608\uc81c","date":"2014-12-18","format":false,"excerpt":"java mail API\ub97c \uc774\uc6a9\ud558\uc5ec GMail\uc758 SMTP\ub97c \uc774\uc6a9\ud558\uc5ec \uba54\uc77c\uc744 \ubc1c\uc1a1\ud558\ub294 \uac83\uc5d0 \ub300\ud574 \uc544\uc8fc \uac04\ub7b5\ud558\uac8c \uc54c\uc544\ubcf8\ub2e4. \uba54\uc77c\uc744 \ubc1c\uc1a1\ud558\uae30 \uc704\ud574\uc11c\ub294 Java Mail API\uc640 GMail \uacc4\uc815\uc774 \ud544\uc694\ud558\ub2e4. (GMail SMTP\ub294 SSL\ub85c \uacc4\uc815 \uc778\uc99d\uc744 \ud574\uc57c \uc0ac\uc6a9\uc774 \uac00\ub2a5\ud558\ub2e4.) \ubc1c\uc1a1\ub418\ub294 \uba54\uc77c\uc758 \ud14d\uc2a4\ud2b8\ub294 HTML\uc774\uba70, UTF-8\uc774\ub2e4. Text\ub098 \ub2e4\ub978 \uce90\ub9ad\ud130\uc14b\uc744 \uc6d0\ud55c\ub2e4\uba74 \uc870\uae08 \uc218\uc815\ud558\uba74 \ub41c\ub2e4. \uae30\ub2a5\uc740 \ucca8\ubd80\ud30c\uc77c\uc774 \uc788\ub294 \uba54\uc77c\uacfc \uc5c6\ub294 \uba54\uc77c\ub9cc \uad6c\ubd84\ud558\uc5ec\u2026","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":214,"url":"https:\/\/blog.box.kr\/?p=214","url_meta":{"origin":583,"position":3},"title":"UTF-8 \uc778\ucf54\ub529\ub41c \ud30c\uc77c \uc77d\uace0 EUC-KR \ubcc0\uacbd encoded file read","date":"2014-07-22","format":false,"excerpt":"package com.javawide.files; import java.io.*; public class UTF8Reader { public static void main(String[] args) { UTF8Reader reader = new UTF8Reader(); try { String utf8String = reader.readFully(\"C:\/utf8test.txt\"); System.out.println(utf8String); System.out.println(new String(utf8String.getBytes(), \"EUC-KR\")); } catch (Exception e) { e.printStackTrace(); } } \u00a0public String readFully(String fileName) throws Exception { File f = new File(fileName); if(!f.exists())\u2026","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":450,"url":"https:\/\/blog.box.kr\/?p=450","url_meta":{"origin":583,"position":4},"title":"[\ud38c]Gmail SMTP Server\ub97c \uc774\uc6a9\ud55c \uba54\uc77c\uc804\uc1a1 \uc608\uc81c","date":"2014-12-18","format":false,"excerpt":"Gmail SMTP Server\ub97c \uc774\uc6a9\ud55c \uba54\uc77c\uc804\uc1a1 \uc608\uc81c (\ubb34\ub8cc SMTP & Authentication) http:\/\/unionbaby.tistory.com\/category\/+%20Programer\/*%20java?page=2 + Programer\/* java Gmail, Authentication in JavaMail JavaMail 1.4.1:http:\/\/java.sun.com\/products\/javamail\/downloads\/index.html JAF 1.1.1: http:\/\/java.sun.com\/products\/javabeans\/jaf\/downloads\/index.html \u00a0 Gmail.java import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; import java.util.*; import java.security.Security; public class Gmail { public static void main(String[] args) { Properties p =\u2026","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":130,"url":"https:\/\/blog.box.kr\/?p=130","url_meta":{"origin":583,"position":5},"title":"Simple Spring Quartz Web App with Maven and Eclipse","date":"2014-06-23","format":false,"excerpt":"Simple Spring Quartz Web App with Maven and Eclipse 1. Create a Maven Web App project with Eclipse File -> New -> Project -> Other -> Maven Project -> Next -> Next -> You should be at the Select Archtype Screen. Type \"webapp\" (without the quotes) in the \"filter\" textbox.\u2026","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/2.bp.blogspot.com\/-jQPX5aFgXCI\/UEz7N6HbfRI\/AAAAAAAAAIk\/rRh1qFCkcJk\/s320\/1-MavenProj.PNG?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/583"}],"collection":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=583"}],"version-history":[{"count":0,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/583\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}