package com.java.myapp; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class MyClass { public static void main(String[] args) { try{ byte[] buffer = new byte[1024]; FileOutputStream fo = new FileOutputStream("C:\\java\\myZip.zip"); ZipOutputStream zo = new ZipOutputStream(fo); ZipEntry ze; FileInputStream in; int len; //*** file 1 ze = new ZipEntry("data1.txt"); zo.putNextEntry(ze); in = new FileInputStream("C:\\java\\data1.txt"); while ((len = in.read(buffer)) > 0) { zo.write(buffer, 0, len); } in.close(); //*** file 2 ze = new ZipEntry("data2.txt"); zo.putNextEntry(ze); in = new FileInputStream("C:\\java\\data2.txt"); while ((len = in.read(buffer)) > 0) { zo.write(buffer, 0, len); } in.close(); zo.closeEntry(); zo.close(); fo.close(); System.out.println("Create Zip Done"); }catch(IOException ex){ ex.printStackTrace(); } } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท