`
hanqunfeng
  • 浏览: 1527697 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA压缩与解压缩--zip

    博客分类:
  • JAVA
阅读更多

通过java打zip包或者解压zip包,没什么特别的,jdk已经提供了API。

 

一。批量文件打压缩与解压缩,打好的zip包可以使用winrar进行解压

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.netqin.servlet.CommonServlet;

//import org.apache.tools.zip.ZipEntry; 
//import org.apache.tools.zip.ZipOutputStream; 
/** 注释乱码。 经过反复测试,发现中文支持有问题。 
 * google了一下解决方案,用ant包中的两个类 //import org.apache.tools.zip.ZipEntry; //import org.apache.tools.zip.ZipOutputStream;
 *  替换Java包的对应的两个类 import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; 即可完美支持中文。
 */

/** * Java版的Zip工具 * */
public class ZipTool {
	private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

	/**
	 * * 批量压缩文件(夹) * 
	 * * @param resFileList 要压缩的文件(夹)列表 
	 * * @param zipFile 生成的压缩文件 
	 * * @throws
	 * IOException 当压缩过程出错时抛出
	 */
	public static void zipFiles(List<File> resFileList, File zipFile)
			throws IOException {
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipFile), BUFF_SIZE));
		for (File resFile : resFileList) {
			zipFile(resFile, zipout, "");
		}
		zipout.close();
	}

	/**
	 * * 批量压缩文件(夹) * 
	 * * @param resFileList 要压缩的文件(夹)列表 
	 * * @param zipFile 生成的压缩文件 
	 * * @param comment 压缩文件的注释 
	 * * @throws IOException 当压缩过程出错时抛出
	 */
	public static void zipFiles(List<File> resFileList, File zipFile,
			String comment) throws IOException {
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipFile), BUFF_SIZE));
		for (File resFile : resFileList) {
			zipFile(resFile, zipout, "");
		}
		zipout.setComment(comment);
		zipout.close();
	}
	
	/**                                                          
	* 描述 : <将文件写入压缩包中>. <br>
	*<p>                                                 
	                                                                                                                                                                                                      
	* @param resFile
	* @param zipout
	* @param rootpath
	* @throws IOException                                                                                    			   
	*/
	private static void zipFile(File resFile, ZipOutputStream zipout,
			String rootpath) throws IOException {
		rootpath = rootpath
				+ (rootpath.trim().length() == 0 ? "" : File.separator)
				+ resFile.getName();
		if (resFile.isDirectory()) {
			File[] fileList = resFile.listFiles();
			for (File file : fileList) {
				zipFile(file, zipout, rootpath);
			}
		} else {
			byte buffer[] = new byte[BUFF_SIZE];
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(resFile), BUFF_SIZE);
			zipout.putNextEntry(new ZipEntry(rootpath));
			int realLength;
			while ((realLength = in.read(buffer)) != -1) {
				zipout.write(buffer, 0, realLength);
			}
			in.close();
			zipout.flush();
			zipout.closeEntry();
		}
	}

	/**
	 * * 解压缩一个文件 * 
	 * * @param zipFile 压缩文件 
	 * * @param folderPath 解压缩的目标目录 
	 * * @throws
	 * IOException 当压缩过程出错时抛出
	 */
	@SuppressWarnings("unchecked")
	public static void upZipFile(File zipFile, String folderPath)
			throws IOException {
		ZipFile zf = new ZipFile(zipFile);
		for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
			ZipEntry entry = ((ZipEntry) entries.nextElement());
			InputStream in = zf.getInputStream(entry);
			OutputStream out = new FileOutputStream(folderPath + File.separator
					+ entry.getName());
			byte buffer[] = new byte[BUFF_SIZE];
			int realLength;
			while ((realLength = in.read(buffer)) > 0) {
				out.write(buffer, 0, realLength);
			}
			in.close();
			out.close();
		}
	}

	

	
	public static void main(String[] args) throws IOException {
		List resFileList = new ArrayList();
		resFileList.add(new File("C:\\new.gif"));
		resFileList.add(new File("C:\\HelloWorld.java"));
		resFileList.add(new File("C:\\crebas.sql"));
		resFileList.add(new File("E:\\log.log"));
		resFileList.add(new File("C:\\ooo\\upx\\"));
		File zipFile = new File("C:\\txxxt.zip");
		ZipUtils.zipFiles(resFileList, zipFile);
	}

}

 二.流压缩及解压缩

	/**                                                          
	* 描述 : <将流打包到文件>. <br>
	*<p>                                                 
	                                                                                                                                                                                                      
	* @param b 数据流
	* @param filename 压缩包文件路径
	* @return
	* @throws IOException                                                                                    			   
	*/
	public static String makeZipFile(byte[] b, String filename)
			throws IOException {
		File file = new File(filename+".zip");
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(file), BUFF_SIZE));
		zipout.putNextEntry(new ZipEntry(filename));
		zipout.write(b);

		zipout.flush();
		zipout.closeEntry();
		zipout.setComment("");
		zipout.close();

		return fileNewName;
	}

	

	/**                                                          
	* 描述 : <获得文件的数据流>. <br>
	*<p>                                                 
	                                                                                                                                                                                                      
	* @param filename 文件路径
	* @return
	* @throws FileNotFoundException                                                                                    			   
	*/
	public static byte[] getZipFileByte(String filename)
			throws FileNotFoundException {
		File file = new File(filename);
		byte buffer[] = new byte[BUFF_SIZE];
		InputStream stream = null;
		byte[] data = null;

		try {
			if (file.canRead()) {

				stream = new FileInputStream(file);
				data = new byte[Integer.parseInt(String.valueOf(file.length()))];
				stream.read(data);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;

		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();

				}
			}
		}

		return data;
	}

	

	/**
	 * 描述 : <对数据流进行压缩,并返回压缩后的数据流>. <br>
	 *<p>
	 * 
	 * @param b
	 * @return
	 */
	public byte[] getZipByte(byte[] b) {

		byte[] compressed = null;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try {
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(b);
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (zout != null) {
				try {

					zout.close();
				} catch (IOException e) {
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}
		return compressed;
	}

	/**
	 * 将压缩后的 byte[] 数据解压缩
	 * 
	 * @param compressed
	 *            压缩后的 byte[] 数据
	 * @return 解压后的字符串
	 * @throws ClassNotFoundException
	 */
	public static final byte[] decompress(byte[] compressed) {

		if (compressed == null)
			return null;

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		byte[] decompressed = null;
		try {
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);

			ZipEntry entry = zin.getNextEntry();

			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}

			decompressed = out.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
			decompressed = null;
		} finally {
			if (zin != null) {
				try {
					zin.close();
				} catch (IOException e) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}

		return decompressed;
	}

 说明一下,在对流进行压缩时,如果最后没有将流写入到文件里,而是将流直接吐出,这样生成的文件将不能通过winrar进行解压,只能通过java读流的方式解压。

分享到:
评论
1 楼 浪子柔情 2011-07-12  


  lz您好 ,你上线后 麻烦你加下我的QQ358846415

  就您上面写的批量压缩文件  我有个问题想要请教您   很急  谢谢 @!

等你回信

相关推荐

Global site tag (gtag.js) - Google Analytics