用jdk api读写zip文件

原来在jdk6中读写zip会有中文乱码问题。但是在jdk7之后,读写zip可以设置charset编码了。


ZipFile和ZipOutputStream都多了个含有charset参数的构造函数。


以下是写一个zip文件的例子

package com.memorycat.learn.learnjdk.java.util.zip;

import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author MemoryCat.com
 * @version 2016年3月8日
 */

public class MyZipOutputStream {
	public static void main(String[] args) throws Exception {
		ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("c:/3.zip"),Charset.forName("GBK"));
		zipOutputStream.setLevel(9);//压缩级别
		zipOutputStream.setComment("这是备注,该zip文件由MemoryCat生成!");
		
		zipOutputStream.putNextEntry(new ZipEntry("根目录文件"));
		zipOutputStream.write("我是根目录文件的内容".getBytes("GBK"));
		zipOutputStream.closeEntry();
		
		zipOutputStream.putNextEntry(new ZipEntry("根目录文件2"));
		zipOutputStream.write("我是根目录文件的内容2".getBytes("GBK"));
		zipOutputStream.closeEntry();
		
		zipOutputStream.putNextEntry(new ZipEntry("根目录文件/a.txt"));
		zipOutputStream.write("aaaaa2".getBytes("GBK"));
		zipOutputStream.closeEntry();
		
		zipOutputStream.putNextEntry(new ZipEntry("a/b/c/d/e.txt"));
		zipOutputStream.write("eeeeeeeeeeeeee".getBytes("GBK"));
		zipOutputStream.closeEntry();
		
		zipOutputStream.putNextEntry(new ZipEntry("a/b/w/saf.txt"));
		zipOutputStream.write("aaabsad".getBytes("GBK"));
		zipOutputStream.closeEntry();
		
		
		zipOutputStream.close();
		
		System.out.println("end");
	}
}


以下是一个读zip文件的例子

package com.memorycat.learn.learnjdk.java.util.zip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author MemoryCat.com
 * @version 2016年3月7日
 */

public class MyZipFile {
	public static void main(String[] args) throws IOException {

//		SortedMap<String, Charset> availableCharsets = Charset.availableCharsets();
//		for (Entry<String, Charset> e : availableCharsets.entrySet()) {
//			System.out.println(e.getKey() + "->" + e.getValue());
//		}
//		System.out.println("-----------------");
		ZipFile zipFile = new ZipFile("c:/3.zip", Charset.forName("GBK"));
		System.out.println(zipFile);
		System.out.println(zipFile.getName());
		System.out.println(zipFile.size());
		Enumeration<? extends ZipEntry> entries = zipFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry e = entries.nextElement();
			System.out.println(e);
			
			//读取e.txt的内容
			if(e.getName().endsWith("e.txt")){
				InputStream inputStream = zipFile.getInputStream(e);
				BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
				System.out.println("\te.txt----->"+reader.readLine());
				reader.close();
			}
		}
		
		
		
	}
}


读取输出结果

java.util.zip.ZipFile@bda96b
c:\3.zip
5
根目录文件
根目录文件2
根目录文件/a.txt
a/b/c/d/e.txt
	e.txt----->eeeeeeeeeeeeee
a/b/w/saf.txt

评论
2021-05-11 01:06:22 2
文章信息
作者: admin
目录: java
创建: 2016-03-08
更新: 2016-03-08
如果文章对你有帮助,请博主喝杯咖啡:-)