org.apache.tools.zip.ZipFile Java Examples

The following examples show how to use org.apache.tools.zip.ZipFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileUtil.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * unzip zip file
 *
 * @param zipPath
 * @param targetDir
 * @throws IOException
 */
public static void unzipFiles(String zipPath, String targetDir) throws IOException {

	try (ZipFile zf = new ZipFile(new File(zipPath))) {
		Enumeration<ZipEntry> en = zf.getEntries();

		while (en.hasMoreElements()) {
			ZipEntry ze = en.nextElement();
			File f = new File(targetDir + ze.getName());
			if (ze.isDirectory()) {
				f.mkdirs();
			} else {
				writerUnzipFile(zf, ze, f);
				printInfoLog(ze.getName());
			}
		}

	} catch (IOException e) {
		
		throw e;
	}
	printInfoLog("***********************Unzip Complete***************************");
}
 
Example #2
Source File: FileUtil.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
private static void writerUnzipFile(ZipFile zf, ZipEntry ze, File f) throws IOException {

		int length = 0;
		byte[] b = new byte[2048];
		if (!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}

		try (OutputStream outputStream = new FileOutputStream(f); InputStream inputStream = zf.getInputStream(ze)) {
			while ((length = inputStream.read(b)) > 0) {
				outputStream.write(b, 0, length);
			}
		} catch (IOException e) {
			throw e;
		}

	}
 
Example #3
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param zipFile
 * @param destinationPath
 * @param overwrite
 * @param transformer
 * @param monitor
 * @return
 * @throws IOException
 */
public static IStatus extract(File zipFile, File destinationPath, Conflict overwrite,
		IInputStreamTransformer transformer, IProgressMonitor monitor) throws IOException
{
	if (canDoNativeUnzip(overwrite, transformer))
	{
		return nativeUnzip(zipFile, destinationPath, overwrite, monitor);
	}

	ZipFile zip = new ZipFile(zipFile);
	return extract(zip, zip.getEntries(), destinationPath, overwrite, transformer, monitor);
}
 
Example #4
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Open input stream for specified zip entry.
 * 
 * @param zipFile
 * @param path
 * @return
 * @throws IOException
 */
public static InputStream openEntry(File zipFile, IPath path) throws IOException
{
	ZipFile zip = new ZipFile(zipFile);
	ZipEntry entry = zip.getEntry(path.makeRelative().toPortableString());
	if (entry != null)
	{
		return zip.getInputStream(entry);
	}
	return null;
}
 
Example #5
Source File: ZipUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 解压文件到指定目录
 * 
 * @param zipFile
 * @param descDir
 * @author isea533
 */
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile, String descDir)
		throws IOException {
	File pathFile = new File(descDir);
	if (!pathFile.exists()) {
		pathFile.mkdirs();
	}
	ZipFile zip = new ZipFile(zipFile);
	for (Enumeration entries = zip.getEntries(); entries.hasMoreElements();) {
		ZipEntry entry = (ZipEntry) entries.nextElement();
		String zipEntryName = entry.getName();
		InputStream in = zip.getInputStream(entry);
		String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");
		outPath = new String(outPath.getBytes("utf-8"), "ISO8859-1");
		;
		// 判断路径是否存在,不存在则创建文件路径
		File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
		if (!file.exists()) {
			file.mkdirs();
		}
		// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
		if (new File(outPath).isDirectory()) {
			continue;
		}
		// 输出文件路径信息
		//System.out.println(outPath);

		OutputStream out = new FileOutputStream(outPath);
		byte[] buf1 = new byte[1024];
		int len;
		while ((len = in.read(buf1)) > 0) {
			out.write(buf1, 0, len);
		}
		in.close();
		out.close();
	}
}
 
Example #6
Source File: ZipFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public DetailsImpl(ZipEntry entry, ZipFile zip, AtomicBoolean stopFlag, Chmod chmod) {
    super(chmod);
    this.entry = entry;
    this.zip = zip;
    this.stopFlag = stopFlag;
}
 
Example #7
Source File: ZipFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public DetailsImpl(ZipEntry entry, ZipFile zip, AtomicBoolean stopFlag, Chmod chmod) {
    super(chmod);
    this.entry = entry;
    this.zip = zip;
    this.stopFlag = stopFlag;
}
 
Example #8
Source File: ZipFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public DetailsImpl(ZipEntry entry, ZipFile zip, AtomicBoolean stopFlag, Chmod chmod) {
    super(chmod);
    this.entry = entry;
    this.zip = zip;
    this.stopFlag = stopFlag;
}
 
Example #9
Source File: ZipFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public DetailsImpl(ZipEntry entry, ZipFile zip, AtomicBoolean stopFlag, Chmod chmod) {
    super(chmod);
    this.entry = entry;
    this.zip = zip;
    this.stopFlag = stopFlag;
}
 
Example #10
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Do the dirty work of actually extracting a {@link ZipEntry} to it's destination.
 * 
 * @param zip
 * @param entry
 * @param destinationPath
 * @param transformer
 * @param conflicts
 * @param howToResolve
 * @param monitor
 * @return
 */
private static IStatus extractEntry(ZipFile zip, ZipEntry entry, File destinationPath,
		IInputStreamTransformer transformer, Conflict howToResolve, IProgressMonitor monitor)
{
	// Return early since this is only supposed to handle files.
	if (entry.isDirectory())
	{
		return Status.OK_STATUS;
	}

	SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
	String name = entry.getName();
	File file = new File(destinationPath, name);
	if (IdeLog.isInfoEnabled(CorePlugin.getDefault(), IDebugScopes.ZIPUTIL))
	{
		IdeLog.logInfo(CorePlugin.getDefault(),
				MessageFormat.format("Extracting {0} as {1}", name, file.getAbsolutePath()), IDebugScopes.ZIPUTIL); //$NON-NLS-1$
	}
	subMonitor.setTaskName(Messages.ZipUtil_extract_prefix_label + name);
	subMonitor.worked(2);
	try
	{
		if (file.exists())
		{
			switch (howToResolve)
			{
				case OVERWRITE:
					if (IdeLog.isInfoEnabled(CorePlugin.getDefault(), IDebugScopes.ZIPUTIL))
					{
						IdeLog.logInfo(
								CorePlugin.getDefault(),
								MessageFormat.format(
										"Deleting a file/directory before overwrite {0}", file.getAbsolutePath()), IDebugScopes.ZIPUTIL); //$NON-NLS-1$
					}
					FileUtil.deleteRecursively(file);
					break;

				case SKIP:
					return Status.OK_STATUS;

				case PROMPT:
					return new Status(IStatus.INFO, CorePlugin.PLUGIN_ID, ERR_CONFLICTS, name, null);
			}
		}
		subMonitor.setWorkRemaining(95);

		extractFile(zip, entry, destinationPath, file, transformer, subMonitor.newChild(95));
	}

	finally
	{
		subMonitor.done();
	}

	return Status.OK_STATUS;
}
 
Example #11
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Extract zip file into specified local path. By default, file that exist in the destination path will not be
 * overwritten.
 * 
 * @param zipFile
 * @param destinationPath
 * @param monitor
 * @throws IOException
 */
public static IStatus extract(File zipFile, File destinationPath, IProgressMonitor monitor) throws IOException
{
	if (canDoNativeUnzip(Conflict.SKIP, null))
	{
		return nativeUnzip(zipFile, destinationPath, Conflict.SKIP, monitor);
	}
	return extract(new ZipFile(zipFile), destinationPath, monitor);
}
 
Example #12
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Extract zip file into specified local path. File that exist in the destination path will be overwritten if the
 * <code>overwrite</code> flag is <code>true</code>. Native unzip mechanism will be attempted if the
 * <code>isNative</code> flag is <code>true</code>
 * 
 * @param zipFile
 * @param destinationPath
 * @param overwrite
 * @param isNative
 * @param monitor
 * @return
 * @throws IOException
 */
public static IStatus extract(File zipFile, File destinationPath, boolean overwrite, boolean isNative,
		IProgressMonitor monitor) throws IOException
{
	Conflict whatToDo = overwrite ? Conflict.OVERWRITE : Conflict.SKIP;
	if (canDoNativeUnzip(whatToDo, null) && isNative)
	{
		return nativeUnzip(zipFile, destinationPath, whatToDo, monitor);
	}
	return extract(new ZipFile(zipFile), destinationPath, overwrite, monitor);
}
 
Example #13
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Extract zip file into specified local path. File that exist in the destination path will be overwritten if the
 * <code>overwrite</code> flag is <code>true</code>.
 * 
 * @param zip
 * @param destinationPath
 * @param overwrite
 * @param monitor
 * @throws IOException
 */
private static IStatus extract(ZipFile zip, File destinationPath, boolean overwrite, IProgressMonitor monitor)
		throws IOException
{
	return extract(zip, zip.getEntries(), destinationPath, overwrite ? Conflict.OVERWRITE : Conflict.SKIP, null,
			monitor);
}
 
Example #14
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Extract specified list of entries from zip file to local path. By default, file that exist in the destination
 * path will not be overwritten.
 * 
 * @param zip
 * @param entries
 * @param destinationPath
 * @param monitor
 * @throws IOException
 */
@SuppressWarnings("rawtypes")
private static IStatus extract(ZipFile zip, Enumeration entries, File destinationPath, IProgressMonitor monitor)
		throws IOException
{
	return extract(zip, entries, destinationPath, Conflict.SKIP, null, monitor);
}
 
Example #15
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Extract zip file into specified local path. By default, file that exist in the destination path will not be
 * overwritten.
 * 
 * @param zip
 * @param destinationPath
 * @param monitor
 * @throws IOException
 */
private static IStatus extract(ZipFile zip, File destinationPath, IProgressMonitor monitor) throws IOException
{
	return extract(zip, zip.getEntries(), destinationPath, monitor);
}