JAVA 读取zip文档方式二:使用 ZipFile

JAVA 读取zip文档方式二:使用 ZipFile

<pre name="code" class="java">package zipTest.com.test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZIPTest {
	private static int BUF_SIZE = 1024*10;
	public static void main(String[] args) {
		try {
			
			//方式2
			ZipFile zf = new ZipFile("D:/img.zip");
			Enumeration<? extends ZipEntry> enu = zf.entries();
			int s = zf.size();
			for (int i = 0; i < s; i++) {
				ZipEntry ent = enu.nextElement();
				String na = null;
				na = ent.getName();
				//获取对象名称
				System.out.println(na);
				//确定文件存放位置
				File fs = new File("D:"+File.separator+"imgsss"+File.separator+na);
				//判断对象是是否是文件夹
				if(ent.isDirectory()){
					fs.mkdirs();
					continue;
				 }
				InputStream zfs = zf.getInputStream(ent);
				//保存文件
				BufferedOutputStream fo =  new BufferedOutputStream(new FileOutputStream(fs));
				byte [] content=new byte[BUF_SIZE];
                int len;
                while((len=zfs.read(content))!=-1){
                	fo.write(content,0,len);
                	fo.flush();
                }
				fo.close();
				zfs.close();
			}
			zf.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 
 

猜你喜欢

转载自blog.csdn.net/wbxx727124/article/details/50708895