当前位置: 移动技术网 > IT编程>开发语言>Java > Java压缩文件工具类ZipUtil使用方法代码示例

Java压缩文件工具类ZipUtil使用方法代码示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

许嘉允,都市风流史,720.ccav.ru

本文实例通过java的zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下:

package com.utility.zip;
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.zip.zipentry;
import java.util.zip.zipinputstream;
import java.util.zip.zipoutputstream;
import com.utility.io.ioutil;
/**
 * 通过java的zip输入输出流实现压缩和解压文件
 * 
 * @author liujiduo
 * 
 */
public final class ziputil {
	private ziputil() {
		// empty
	}
	/**
   * 压缩文件
   * 
   * @param filepath
   *      待压缩的文件路径
   * @return 压缩后的文件
   */
	public static file zip(string filepath) {
		file target = null;
		file source = new file(filepath);
		if (source.exists()) {
			// 压缩文件名=源文件名.zip
			string zipname = source.getname() + ".zip";
			target = new file(source.getparent(), zipname);
			if (target.exists()) {
				target.delete();
				// 删除旧的文件
			}
			fileoutputstream fos = null;
			zipoutputstream zos = null;
			try {
				fos = new fileoutputstream(target);
				zos = new zipoutputstream(new bufferedoutputstream(fos));
				// 添加对应的文件entry
				addentry("/", source, zos);
			}
			catch (ioexception e) {
				throw new runtimeexception(e);
			}
			finally {
				ioutil.closequietly(zos, fos);
			}
		}
		return target;
	}
	/**
   * 扫描添加文件entry
   * 
   * @param base
   *      基路径
   * 
   * @param source
   *      源文件
   * @param zos
   *      zip文件输出流
   * @throws ioexception
   */
	private static void addentry(string base, file source, zipoutputstream zos)
	      throws ioexception {
		// 按目录分级,形如:/aaa/bbb.txt
		string entry = base + source.getname();
		if (source.isdirectory()) {
			for (file file : source.listfiles()) {
				// 递归列出目录下的所有文件,添加文件entry
				addentry(entry + "/", file, zos);
			}
		} else {
			fileinputstream fis = null;
			bufferedinputstream bis = null;
			try {
				byte[] buffer = new byte[1024 * 10];
				fis = new fileinputstream(source);
				bis = new bufferedinputstream(fis, buffer.length);
				int read = 0;
				zos.putnextentry(new zipentry(entry));
				while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
					zos.write(buffer, 0, read);
				}
				zos.closeentry();
			}
			finally {
				ioutil.closequietly(bis, fis);
			}
		}
	}
	/**
   * 解压文件
   * 
   * @param filepath
   *      压缩文件路径
   */
	public static void unzip(string filepath) {
		file source = new file(filepath);
		if (source.exists()) {
			zipinputstream zis = null;
			bufferedoutputstream bos = null;
			try {
				zis = new zipinputstream(new fileinputstream(source));
				zipentry entry = null;
				while ((entry = zis.getnextentry()) != null
				            && !entry.isdirectory()) {
					file target = new file(source.getparent(), entry.getname());
					if (!target.getparentfile().exists()) {
						// 创建文件父目录
						target.getparentfile().mkdirs();
					}
					// 写入文件
					bos = new bufferedoutputstream(new fileoutputstream(target));
					int read = 0;
					byte[] buffer = new byte[1024 * 10];
					while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
						bos.write(buffer, 0, read);
					}
					bos.flush();
				}
				zis.closeentry();
			}
			catch (ioexception e) {
				throw new runtimeexception(e);
			}
			finally {
				ioutil.closequietly(zis, bos);
			}
		}
	}
	public static void main(string[] args) {
		string targetpath = "e:\\win7壁纸";
		file file = ziputil.zip(targetpath);
		system.out.println(file);
		ziputil.unzip("f:\\win7壁纸.zip");
	}
}

下面是通过io流工具类实现关闭一个或多个流对象的java语言描述,获取可关闭的流对象列表,具体如下:

package com.utility.io;
import java.io.closeable;
import java.io.ioexception;
/**
 * io流工具类
 * 
 * @author liujiduo
 * 
 */
public class ioutil {
	/**
   * 关闭一个或多个流对象
   * 
   * @param closeables
   *      可关闭的流对象列表
   * @throws ioexception
   */
	public static void close(closeable... closeables) throws ioexception {
		if (closeables != null) {
			for (closeable closeable : closeables) {
				if (closeable != null) {
					closeable.close();
				}
			}
		}
	}
	/**
   * 关闭一个或多个流对象
   * 
   * @param closeables
   *      可关闭的流对象列表
   */
	public static void closequietly(closeable... closeables) {
		try {
			close(closeables);
		}
		catch (ioexception e) {
			// do nothing
		}
	}
}

总结

以上就是本文关于java压缩文件工具类ziputil使用方法代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网