這本是別人的東西,我只是修改了中文問題。在這個基礎上改一下就可以壓縮多個文件和目錄,甚至可以寫一個winzip之類的東東哦,有興趣的可以研究一下。
壓縮Sample code:
- import java.io.*;
- import java.util.zip.*;
- /**
- * @version Version 1.3
- */
- public class w0514{
- public static void main(String[] args){
- try{
- BufferedReader in=new BufferedReader(
- new InputStreamReader(new FileInputStream(args[0]),"ISO8859_1"));
- FileOutputStream f=new FileOutputStream(args[1]+".zip");
- CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32());
- ZipOutputStream out=new ZipOutputStream(
- new BufferedOutputStream(ch));
- int c;
- out.putNextEntry(new ZipEntry(args[0]));
- while((c=in.read())!=-1)
- out.write(c);
- in.close();
- out.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
解壓縮Sample code:
Java內建支援ZIP格式檔案的壓縮及解壓縮,透過使用java.util.zip套件,我們可以用來將網頁上傳的ZIP檔案加以解壓縮再做後續處理。這裡我們可以先將解壓縮的固定步驟寫成一個UnZipBean,後續要解壓縮zip時只要先將檔名及解壓縮的目錄傳進去,再呼叫unzip()函式即可。
詳細代碼請到 這裡
下面是經過我改良過的Code:
UnZipBean:
- package gays.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.io.IOException;
- import java.util.Enumeration;
- import java.util.zip.CRC32;
- import java.util.zip.CheckedOutputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipException;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipOutputStream;
- import java.io.*;
- public class UnZipBean {
- public static final int EOF = -1;
- static final int BUFFER = 2048;
- private String zipFile;
- private String targetDirectory;
- private ZipFile zf;
- /** Constructor */
- public UnZipBean() {
- }
- public UnZipBean(String zipFile, String targetDirectory) {
- this.zipFile = zipFile;
- this.targetDirectory = targetDirectory;
- }
- public void setZipFile(String zipFile) {
- this.zipFile = zipFile;
- }
- public String getZipFile() {
- return zipFile;
- }
- public void setTargetDirectory(String targetDirectory) {
- this.targetDirectory = targetDirectory;
- }
- public String getTargetDirectory() {
- return targetDirectory;
- }
- /**
- * BD : According to the input parameters, zip the file/folder indicated by member property:'s' and save
- * into the file indicated by member property:'t'. If the
- * Change History :
- * 2009/09/27 John Lee
- * Release
- * @param s
- * @param t
- * @return
- */
- public boolean zip(String s,String t){
- File source = new File(s);
- File target = new File(t);
- if(!target.exists()){
- try {
- System.out.println("Prepare "+target.getAbsolutePath()+"...");
- target.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(source.exists()){
- try{
- FileOutputStream f=new FileOutputStream(t);
- CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32());
- ZipOutputStream out=new ZipOutputStream(
- new BufferedOutputStream(ch));
- zipFile(out,source);
- out.closeEntry();
- out.close();
- return true;
- }catch(IOException ioe){
- ioe.printStackTrace();
- }
- }else{
- System.err.println("Source File doesn't exist! "+source.getAbsolutePath());
- }
- return false;
- }
- protected boolean zipFile(ZipOutputStream out,File f) throws IOException{
- if(f.exists()){
- if (f.isDirectory()) {
- System.out.print("zipFile: "+f.getAbsolutePath());
- File[] files = f.listFiles();
- if(files.length==0){
- System.out.println("(Empty folder)");
- out.putNextEntry(new ZipEntry(f.getAbsoluteFile()+"/"));
- }else{
- System.out.println("("+files.length+")");
- }
- for(int i=0;i
- zipFile(out,files[i]);
- }
- } else {
- System.out.println("\tZIP "+f.getAbsolutePath()+"...");
- out.putNextEntry(new ZipEntry(f.getAbsolutePath()));
- byte[] buf = new byte[1024];
- int len;
- FileInputStream fis = new FileInputStream(f);
- while ((len = fis.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- fis.close();
- }
- return true;
- }else{
- System.out.println(f.getAbsolutePath()+" not exist!");
- }
- return false;
- }
- /**
- * BD : Unzip the file indicated by member property:'zipFile' to the folder indicated by
- * member property:'targetDirectory'
- * Change History :
- * 2009/09/27 John Lee
- * Release
- * @return
- */
- public boolean unzip() {
- boolean done = false;
- if (zipFile != null) {
- try {
- zf = new ZipFile(zipFile);
- Enumeration enumeration = zf.entries();
- while (enumeration.hasMoreElements()) {
- ZipEntry target = (ZipEntry)enumeration.nextElement();
- System.out.print(target.getName() + " .");
- saveEntry(target);
- System.out.println(". unpacked");
- }
- done = true;
- }
- catch (FileNotFoundException e){
- System.out.println("zipfile not found"+e.getMessage());
- }
- catch (ZipException e){
- System.out.println("zip error..."+e.getMessage());
- }
- catch (IOException e){
- System.out.println("IO error..."+e.getMessage());
- }
- finally {
- try {
- zf.close();
- } catch (IOException e) {
- System.out.println("IO error...Can't close zip file"+e.getMessage());
- }
- }
- }
- return done;
- }
- private void saveEntry(ZipEntry target)
- throws ZipException, IOException {
- try {
- File file = new File(targetDirectory + File.separator + target.getName());
- if (target.isDirectory()) {
- file.mkdirs();
- }
- else {
- InputStream is = zf.getInputStream(target);
- BufferedInputStream bis = new BufferedInputStream(is);
- File dir = new File(file.getParent());
- dir.mkdirs();
- FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- int c;
- byte[] data = new byte[BUFFER];
- while((c = bis.read(data, 0, BUFFER)) != EOF) {
- bos.write(data, 0, c);
- }
- bos.flush();
- bos.close();
- fos.close();
- }
- }
- catch (ZipException e) {
- throw e;
- }
- catch (IOException e) {
- throw e;
- }
- }
- }
參考資料:
http://java.sun.com/developer/technicalArticles/Programming/compression/
http://www.wakhok.ac.jp/~tatsuo/sen97/10shuu/UnZip.java.html
java编程
回覆刪除使用外观和感觉