Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java and Zip > Java Create Zip Compress files



Clound SSD Virtual Server

Java Create Zip Compress files

Java Create Zip Compress files ในการสร้าง Zip ไฟล์ด้วยภาษา Java ในกรณีที่ต้องการสร้างไฟล์ Zip ก็สามารถสามารถสร้างได้แบบง่าย ๆ ด้วย Class ของ ZipEntry และ ZipOutputStream โดยไม่ต้องหา Library อื่น ๆ มาใช้

Java Create Zip Compress files

Java Create Zip Compress files


ในการสร้าง Zip files ด้วย Java เราจะเรียกใช้ Package และ Class ของ java.util.zip.ZipEntry และ java.util.zip.ZipOutputStream

Example การสร้าง Zip ไฟล์ด้วย Java แบบง่าย ๆ

Java Create Zip Compress files

ไฟล์ ที่จะทำการบีบอัด้วย Zip

MyClass.java
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();
    	}
        
	}
	
}

Output

Java Create Zip Compress files

ตัวอย่าง ZIP ที่ได้

Java Create Zip Compress files

ตัวอย่าง ZIP ที่ได้






   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-26 11:51:11 / 2013-08-19 11:03:16
  Download : No files
 Sponsored Links / Related

 
Java Extract Zip Decompress/ Create Zip files (Zip4j)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่