Here you can find the source of unzipFile(String fileName, String targetPath)
Parameter | Description |
---|---|
fileName | a parameter |
targetDirName | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void unzipFile(String fileName, String targetPath) throws IOException
//package com.java2s; /**/* w w w . ja va 2 s . c o m*/ * Copyright 2011-2013 BBe Consulting GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; 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; import java.nio.channels.Channels; import java.nio.channels.FileChannel; public class Main { /** * Unzips a zip. * * @param fileName * @param targetDirName * @throws IOException */ public static void unzipFile(String fileName, String targetPath) throws IOException { final File targetDir = new File(targetPath); final ZipFile sourceZip = new ZipFile(fileName); @SuppressWarnings("unchecked") final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) sourceZip.entries(); while (entries.hasMoreElements()) { ZipEntry currentEntry = entries.nextElement(); File targetFile = new File(targetDir, currentEntry.getName()); // create sub directories if needed targetFile.getParentFile().mkdirs(); // write file if it's not a directory if (!currentEntry.isDirectory()) { writeFileFromZip(targetFile, currentEntry, sourceZip); } } } private static void writeFileFromZip(File targetFile, ZipEntry zipEntry, ZipFile archive) throws IOException { FileChannel output = null; FileOutputStream rawOut = null; try { final InputStream rawIn = archive.getInputStream(zipEntry); rawOut = new FileOutputStream(targetFile); output = rawOut.getChannel(); output.transferFrom(Channels.newChannel(rawIn), 0, zipEntry.getSize()); } finally { if (output != null) { output.close(); } if (rawOut != null) { rawOut.close(); } } } }