Java ZIP

声明:本文属于原创作品,转载请注明出处。


本文是关于Java语言对于压缩文件ZIP的一些操作,可以通过流对象ZipInputStream来操作,也可以通过文件对象ZipFile来操作。其中,使用文件对象读写的时候,用到了高数缓存(Cache),文件只打开一次,被重复使用。如果操作过程中,ZIP文件不是经常变化,选择ZipFile性能比较好,如果经常变化在,则更适合选ZipInputStream。具体情况应该具体分析。


下面是一个示例代码,欢迎指正,交流。


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * Examples about ZIP Stream
 * 
 * @author bingduanLin
 * 
 */
public class ZipOutputStreamDemo {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		FileOutputStream fo = new FileOutputStream("test.zip");
		ZipOutputStream zo = new ZipOutputStream(fo);
		zo.setComment("Comment, where will you be?");

		/*
		 * putNextEntry方法: 该方法首先检查是否有Entry是激活的,如果有,关闭它。将ZipOutputStream流
		 * 定位到下一条Entry的地方,开始写入数据。 Begins writing a new zip entry and positions
		 * the stream to the start of the entry data. close current if it is
		 * active
		 */

		int byteSize = 0;
		for (int i = 0; i < 5; i++) {
			ZipEntry ze = new ZipEntry(i + ".txt");
			ze.setComment("Comment for each entry!");
			zo.putNextEntry(ze); // called by ZipOutputStream
			String name = "林泽亚";
			byte[] bytes = Encode(name);
			byteSize = bytes.length;
			zo.write(bytes);
			zo.closeEntry();
		}

		zo.close();
		// 上述代码最好封装在一个方法中

		readZip("test.zip", byteSize);
		readZipByZipFile("test.zip", byteSize);
	}

	/**
	 * @param zipName
	 *            the name of zip file to be readed
	 * @throws IOException
	 */
	private static void readZip(String zipName, int byteSize)
			throws IOException {
		ZipInputStream zi = new ZipInputStream(new FileInputStream(zipName));
		ZipEntry entry;
		while ((entry = zi.getNextEntry()) != null) {
			System.out.println(entry.getName());
			byte[] b = new byte[byteSize];
			zi.read(b);
			System.out.print(decode(b) + ":");
			for (byte by : b) {
				System.out.printf("%X ", by);
			}
			System.out.println();
			zi.closeEntry();
		}
		zi.close();

	}

	/**
	 * @param zipName
	 * @param byteSize
	 * @throws IOException
	 * @throws ZipException
	 */
	private static void readZipByZipFile(String zipName, int byteSize)
			throws ZipException, IOException {
		ZipFile zf = new ZipFile(new File("test.zip"));
		ZipEntry entry = zf.getEntry("0.txt");
		InputStream is = zf.getInputStream(entry);
		byte[] bytes = new byte[byteSize];
		is.read(bytes);
		System.out.println(decode(bytes));
		zf.close();
	}

	/**
	 * @param source
	 *            String to be encoded
	 * @return byte array presentation of "source"
	 */
	private static byte[] Encode(String source) {

		ByteBuffer bf = charset.encode(source);
		return bf.array();
	}

	/**
	 * @param bytes
	 *            bytes to be decoded
	 * @return the String of bytes
	 */
	private static String decode(byte[] bytes) {
		ByteBuffer bf = ByteBuffer.wrap(bytes);
		CharBuffer cf = charset.decode(bf);
		return cf.toString();
	}

	private static Charset charset = Charset.forName("GBK");

}


你可能感兴趣的:(java,java,java,zip,zip,zip)