자바 zip 압축 및 압축 풀기

3616 단어
ZipCompressor
package com.liam.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author [email protected] 
 * Zip    
 */
public class ZipCompressor {

	public ZipCompressor() {
	}

	public void zipResult(String[] srcFiles, String desFile) {

		boolean isSuccessful = zipCompress(srcFiles, desFile);
		if (isSuccessful) {
			System.out.println("      ,    :【" + desFile + "】。");
		} else {
			System.out.println("(→_→)       。");
		}

	}

	public boolean zipCompress(String[] srcFiles, String desFile) {

		boolean isSuccessful = false;
		String[] fileNames = new String[srcFiles.length];
		for (int i = 0; i < srcFiles.length; i++) {
			fileNames[i] = parse(srcFiles[i]);
		}
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(desFile));
			ZipOutputStream zos = new ZipOutputStream(bos);
			String entryName = null;
			entryName = fileNames[0];

			for (int i = 0; i < fileNames.length; i++) {
				entryName = fileNames[i];

				//   Zip  
				ZipEntry entry = new ZipEntry(entryName);
				zos.putNextEntry(entry);

				BufferedInputStream bis = new BufferedInputStream(
						new FileInputStream(srcFiles[i]));
				byte[] b = new byte[1024];
				while (bis.read(b, 0, 1024) != -1)
					zos.write(b, 0, 1024);
				bis.close();
				zos.closeEntry();

			}
			zos.flush();
			zos.close();
			isSuccessful = true;

		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("             。");
		}
		return isSuccessful;
	}

	//      
	private String parse(String srcFile) {
		int location = srcFile.lastIndexOf("/");
		String fileName = srcFile.substring(location + 1);
		return fileName;
	}

}

UnZip
package com.liam.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author [email protected]
 * Zip    
 */
public class UnZip {

	public UnZip() {}
	
	public void unZipResult(String srcFile){
		boolean isSuccessFul = unzip(srcFile);
		if (isSuccessFul) {
			System.out.println("      。");
		} else {
			System.out.println("(→_→)       。");
		}
	}
	
    public boolean unzip(String srcZipFile) {
         boolean isSuccessful = true;
         try {
              BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcZipFile));
              ZipInputStream zis = new ZipInputStream(bis);

              BufferedOutputStream bos = null;

              //byte[] b = new byte[1024];
              ZipEntry entry = null;
              while ((entry=zis.getNextEntry()) != null) {
                   String entryName = entry.getName();
                   bos = new BufferedOutputStream(new FileOutputStream(entryName));
                   int b = 0;
                   while ((b = zis.read()) != -1) {
                        bos.write(b);
                   }
                   bos.flush();
                   bos.close();
              }
              zis.close();
         } catch (IOException e) {
              isSuccessful = false;
              e.printStackTrace();
              System.out.println("             。");
         }
         return isSuccessful;
    }
}

좋은 웹페이지 즐겨찾기