Java实用小工具--Unzip

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

import org.apache.commons.io.FileUtils;


public class Unzip{
	//return the file path after unzip the zip file
	public static String unZipAndReturnFileName(File file, String outputFolder) throws Exception {
		String destFile = null;
		File folder = new File(outputFolder);
		if (folder.exists()) {
			FileUtils.deleteDirectory(folder);
			folder.mkdirs();
		} else {
			folder.mkdirs();
		}
		ZipFile zipFile = new ZipFile(file);
		Enumeration<? extends ZipEntry> entries = zipFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry entry = entries.nextElement();
			if (!entry.isDirectory()) {
				String fileName = entry.getName();
				String outFileName = outputFolder + File.separator + fileName;
				destFile = outFileName;
				ZipEntry zipEntry = zipFile.getEntry(fileName);
				if (zipEntry != null) {
					InputStream inputStream = zipFile.getInputStream(zipEntry);
					saveToFile(inputStream, outFileName);
				}
			} else {
				String folderPath = outputFolder + File.separator + entry.getName();
				File dir = new File(folderPath);
				dir.mkdir();
			}
		}
		zipFile.close();
		return destFile;
    }
	
	public static void saveToFile(InputStream uploadedInputStream, String sourceFilePath) throws IOException {
        FileUtils.copyInputStreamToFile(uploadedInputStream, new File(sourceFilePath));
    }
}

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