天天看點

java實作簡單的壓縮和解壓縮檔案(包含多級檔案夾和空檔案夾)

最近在學習java的io知識,寫了一個簡單地壓縮和解壓縮的程式,隻能實作對文字型(像txt,world)檔案的壓縮,無法對圖檔等壓縮,在這個過程中遇到的最大困難是空目錄的處理,下面看代碼

實作壓縮檔案

package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipOutputStreamDemo {
	private File file;
	private File zipFile;
	private InputStream input;
	private ZipOutputStream zipOut;
	
	//構造函數
	public ZipOutputStreamDemo(File file) throws FileNotFoundException {
		this.file = file;
		this.zipFile = new File (file.getAbsolutePath()+".zip");
		InputStream input = null;
		this.zipOut = new ZipOutputStream(new FileOutputStream(zipFile));	
	}

	//實作對檔案(夾)的壓縮
	public  void compressFile (File directory) throws IOException {
		if (directory != null) {
			if (directory.isDirectory()) {
				File [] f = directory.listFiles();
				if (f!=null) {
					//目錄裡面檔案數為0,即空目錄,此時在壓縮包中相對路徑下建立一個空目錄
					if (f.length==0) {
						this.zipOut.putNextEntry(new ZipEntry (this.getPath(directory)+File.separator));
					}
					//遞歸調用compressFile函數,找出所有的檔案
					else {
						for (int i=0;i<f.length;i++) {
							compressFile (f[i]);
						}
					}
				}
			}
			//對找出的檔案進行壓縮處理
			else {
				System.out.println(directory);
				this.input = new FileInputStream (directory);
				this.zipOut.putNextEntry(new ZipEntry (this.getPath(directory)));
				int temp = 0;
				while ((temp=input.read())!=-1) {
					this.zipOut.write(temp);
				}
				this.input.close();
				
			}
		}
	}
	
	//獲得該檔案在壓縮包中的相對路徑
	public String getPath (File f) {
		String str1 = this.file.getAbsolutePath();
		int n1= str1.length();
		String str2 = f.getAbsolutePath();
		int n2= str2.length();
		String str3 = this.file.getName();
		int n3= str3.length();
		String str = str2.substring(n1-n3, n2);
		return str;	
	}
}

           

實作檔案的解壓縮

package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ZipInputStreamDemo {
	private File file;
	private File outFile;
	private ZipInputStream zipInput;
	private ZipFile zipFile;	
	private ZipEntry entry;
	private InputStream input;
	private OutputStream out;
	//構造函數
	public ZipInputStreamDemo(File file) throws ZipException, IOException {
		this.file = file;
		this.outFile = null;
		this.zipFile = new ZipFile (file);
		this.zipInput = new ZipInputStream(new FileInputStream(this.file));
		this.entry = null;
		this.input=null;
		this.out=null;
	}
	//對壓縮包中的檔案進行解壓縮
	public void decompress () throws IOException {
		//用getNextEntry獲得壓縮包中的下一個檔案條目并解壓縮,直到沒有檔案了結束循環
		while ((this.entry=this.zipInput.getNextEntry())!=null) {
			System.out.println("解壓縮"+this.entry.getName()+"檔案");
			this.outFile = new File ("e:"+File.separator+this.entry.getName());
			//如果父路徑的檔案夾不存在,就建立該檔案夾
			if (!this.outFile.getParentFile().exists()) {
				this.outFile.getParentFile().mkdirs();
			}
			if (!outFile.exists()) {
				//當該檔案條目為一個空目錄時,建立該空目錄
				if (this.entry.getName().substring(this.entry.getName().length()-1,this.entry.getName().length()).equals(File.separator)) {
					outFile.mkdirs();
				}
				//在解壓的路徑下建立該檔案條目對應的檔案
				else {
					this.outFile.createNewFile();
				}
			}
			if (!outFile.isDirectory()) {
				this.input = this.zipFile.getInputStream(entry);
				this.out=new FileOutputStream (this.outFile);
				int temp = 0;
				while ((temp=input.read())!=-1) {
					out.write(temp);
				}
				this.input.close();
				this.out.close();
			}
		}
	}
}

           

代碼親測在eclipse下運作有效