MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch News Php Tips Ubuntu Spring+Strut+Hibernate Android Programming Design Pattern By PHP C# Design Pattern Linux Quick Tips C# Tips & Technique C# using Linq น่าใช้จริงๆ Java & JavaScript Tips MAVEN Database & SQL ZengCode Framework Guide Mac OSx Zeng Code Code Programming IPhone (Tips and Trick)
Download เอกสารที่น่าสนใจ




|
Unzipping a file from InputStream and returning another InputStream in Java (2011-06-14)
ตัวอย่างนี้เป็นการอ่านไฟล์เป็น FileInputStream แล้วแปลงเป็น ZipInputStream แล้ว unzip Entry ใน zip file ออกมา
import java.io.FileInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;
public class Main { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("c:/zeng.zip"); ZipInputStream zis = new ZipInputStream(fis); ZipEntry entry; // while there are entries I process them while ((entry = zis.getNextEntry()) != null) { System.out.println("entry: " + entry.getName() + ", " + entry.getSize()); // consume all the data from this entry while (zis.available() > 0) zis.read(); // I could close the entry, but getNextEntry does it automatically // zis.closeEntry() } } }
|
อีกตัวอย่างง่ายๆ
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception {
String zipname = "data.zip";
FileInputStream fis = new FileInputStream(zipname);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}
zis.close();
fis.close();
}
}
|
ไม่ยากเลยโค้ดไม่มีอะไรซับซ้อนเลยนะครับ ขอให้สนุกกับจาวานะครับ
|
Comment
|
|