---
title: "FileUpload文件上传组件与java.util.zip解压缩类"
author: "water84222"
date: "2008-08-14T15:54:41Z"
tags: []
slug: "fileupload-java-util-zip"
draft: false
lastmod: "2008-08-14T15:54:41Z"
id: 228044
url: "https://onlyLove.iteye.com/blog/228044"
---

**使用总结
/**
 *要完成的功能，从页面上传zip文件及若干参数，将zip文件解压缩放到指定位置
 *使用到的组件有文件长传组件FileUpload，java类，java.util.zip;
/
<form action="fileUploadFinish.jsp"  name="fileform" method="post" enctype="multipart/form-data">

接受页面的处理
	DiskFileItemFactory dff = new DiskFileItemFactory();
	dff.setSizeThreshold(Integer.parseInt(ZipUploadConfig.getProperty("SIZETHRESHOLD")));
	ServletFileUpload sfu = new ServletFileUpload(dff);
	sfu.setFileSizeMax(Integer.parseInt(ZipUploadConfig.getProperty("FILESIZEMAX")));
	sfu.setSizeMax(Integer.parseInt(ZipUploadConfig.getProperty("SIZEMAX")));

	HashMap param = new HashMap();
	List list = sfu.parseRequest(request);//包括了，form表单中的字段和上传文件

	if (list != null && list.size() > 0) {
		for (int i = 0; i < list.size(); i++) {
			FileItem fi = (FileItem) list.get(i);
			if (fi.isFormField()) { //将参数中的form表单中的字段提取出来
				String name = fi.getFieldName();
				String value = fi.getString();
				param.put(name, value);
			}
		}
	}

	String overload_flag = ""+param.get("overload_flag");//将form表单中的字段取出
	String stylesheet = ""+param.get("stylesheet");
	String template = ""+param.get("template");
	

	if (list != null && list.size() > 0) {
		for (int j = 0; j < list.size(); j++) {
			FileItem fi = (FileItem) list.get(j);
			if (!fi.isFormField() && fi.getName().length() > 0) { //将上传的文件取出
				String zipFileName = fi.getName().substring(fi.getName().lastIndexOf("\\") + 1);//去掉盘符路径，只留下文件名
				//contentInfo.setContenttype(zipFileName.substring(0, zipFileName.lastIndexOf(".")));
				BufferedInputStream in = new BufferedInputStream(fi.getInputStream());
				FileUploader zip = new FileUploader(db, in, contentInfo,servletContext);
				Map result = zip.unzip();
				in.close();
			}
		}
	}

FileUploader.java
	
    public FileUploader(DB db, InputStream inStream,ContentInfoBean contentInfo,ServletContext servletcontext) {
		super();
		this.servletcontext = servletcontext;
		this.inStream = inStream;
		this.db = db;
		this.contentInfo = contentInfo;
	}

    public HashMap unzip() {
		HashMap result = new HashMap();	
        //
        // Expand ZIP file
        //
		ZipInputStream in = new ZipInputStream(this.inStream);
		ZipEntry z;
		Date now = new Date();
		String tmpPath = UploadFileConfig.getUploadTmpPath() + File.separator + now.getTime();//UploadFileConfig.getUploadTmpPath()<--->E:\\project\\V682\\upload\\tmp
		File file = new File(tmpPath);
		if (!file.exists()) {
			try {
				file.mkdirs(); //创建临时文件夹
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			while ((z = in.getNextEntry()) != null) {
				String name = z.getName();
				if (z.isDirectory()) {
					name = name.substring(0, name.length() - 1);
					File f = new File(tmpPath + File.separator + name);
					try {
						f.mkdirs();
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					if (name.indexOf("/") > 0) {
						File folder = new File(tmpPath + File.separator + name.substring(0, name.lastIndexOf("/")));
						if (!folder.exists()) {
							try {
								folder.mkdirs();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					File f = new File(tmpPath + File.separator + name);
					f.createNewFile();
					BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
					int size = 0;
					byte b = new byte;
					while ((size = in.read(b)) != -1) {
						out.write(b, 0, size);
					}
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}**