Java压缩⽂件⼯具类ZipUtil使⽤⽅法代码⽰例
本⽂实例通过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 (ists()) {mkdirs方法
// 压缩⽂件名=源⽂件名.zip
String zipName = Name() + ".zip";
target = new Parent(), zipName);
if (ists()) {
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 {
// 按⽬录分级,形如:/
String entry = base + Name();
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 = ad(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 (ists()) {
ZipInputStream zis = null;
BufferedOutputStream bos = null;
try {
zis = new ZipInputStream(new FileInputStream(source));
ZipEntry entry = null;
while ((entry = NextEntry()) != null
&& !entry.isDirectory()) {
File target = new Parent(), Name());
if (!ParentFile().exists()) {
// 创建⽂件⽗⽬录
}
// 写⼊⽂件
bos = new BufferedOutputStream(new FileOutputStream(target));
int read = 0;
byte[] buffer = new byte[1024 * 10];
while ((read = ad(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 closeables) throws IOException {
if (closeables != null) {
for (Closeable closeable : closeables) {
if (closeable != null) {
closeable.close();
}
}
}
}
/**
* 关闭⼀个或多个流对象
*
* @param closeables
*      可关闭的流对象列表
*/
public static void closeables) {
try {
close(closeables);
}
catch (IOException e) {
// do nothing
}
}
}
总结
以上就是本⽂关于Java压缩⽂件⼯具类ZipUtil使⽤⽅法代码⽰例的全部内容,希望对⼤家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不⾜之处,欢迎留⾔指出。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。