<%@ page import="java.io.FileInputStream" %> <%@ page import="java.io.FileOutputStream" %> <%@ page import="java.io.IOException" %> <%@ page import="java.util.zip.ZipEntry" %> <%@ page import="java.util.zip.ZipOutputStream" %> <html> <head> <title>ThaiCreate.Com JSP Tutorial</title> </head> <body> <% try{ byte[] buffer = new byte[1024]; String zipPath = application.getRealPath("/data/myZip.zip"); FileOutputStream fo = new FileOutputStream(zipPath); ZipOutputStream zo = new ZipOutputStream(fo); ZipEntry ze; FileInputStream in; int len; //*** file 1 ze = new ZipEntry("myData1.txt"); zo.putNextEntry(ze); in = new FileInputStream(application.getRealPath("/data/myData1.txt")); while ((len = in.read(buffer)) > 0) { zo.write(buffer, 0, len); } in.close(); //*** file 2 ze = new ZipEntry("myData2.txt"); zo.putNextEntry(ze); in = new FileInputStream(application.getRealPath("/data/myData2.txt")); while ((len = in.read(buffer)) > 0) { zo.write(buffer, 0, len); } in.close(); zo.closeEntry(); zo.close(); fo.close(); out.println("Create Zip to : " + application.getRealPath("/data/")); }catch(IOException ex){ ex.printStackTrace(); } %> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท