Java 实现zip格式的文件压缩与解压

来源:互联网 发布:mac 蓝牙 编辑:程序博客网 时间:2024/05/17 03:05

学习了两周的IO操作,感觉就对压缩流感兴趣,顺便写点东西练练~.大神请无视~~

1.工具类

package foundation.io.zip;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.charset.Charset;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;/** * zip格式文件压缩与解压 *  * @author Administrator * */public class ZipUtils {//扩展名private static final String ZIP_EXTENSION = ".zip";/** * 压缩  *  * @param srcPath 源路径 * @param descPath 目标路径  null 默认为源路径 * @param descName 目标文件名 null 默认源文件名 * @throws FileNotFoundException * @throws IOException */public static void enZip(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {//数据检测处理if (srcPath == null) throw new FileNotFoundException("源路径为空!");File file = new File(srcPath);if (!file.exists()) throw new FileNotFoundException("源路径不存在!");if (descPath == null || descPath.trim().equals("")) descPath = file.getParent();if (descName == null || descName.trim().equals("")) descName = file.getName();//获取系统文件编码Charset charset = Charset.forName(System.getProperty("file.encoding"));//构建压缩流ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(descPath + File.separator + descName + ZIP_EXTENSION)), charset);//压缩文件enZipDetail(file, zos, "");//关闭资源zos.close();}private static void enZipDetail(File file, ZipOutputStream zos, String parent) throws FileNotFoundException, IOException {int len = 0;ZipEntry entry = null;InputStream is = null;byte[] bytes = new byte[1024];if (file.isDirectory()) {if (!parent.equals("")) parent += File.separator;parent += file.getName();for(File subFile : file.listFiles()) {enZipDetail(subFile, zos, parent);}} else {//初始化输入流is = new FileInputStream(file);//构建ZipEntryif (parent.equals("")) {entry = new ZipEntry(file.getName());} else {entry = new ZipEntry(parent + File.separator + file.getName());}//压缩文件zos.putNextEntry(entry);while((len = is.read(bytes)) != -1) {zos.write(bytes, 0, len);}//关闭资源is.close();}}/** * 解压文件 *  * @param srcPath 源路径 * @param descPath 目标路径 null 默认源路径 * @param descName 解压后的文件夹名  null 默认无文件夹 * @throws FileNotFoundException * @throws IOException */public static void unZip(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {//数据监测处理if (srcPath == null) throw new FileNotFoundException("源路径为空!");File file = new File(srcPath);if (!file.exists()) throw new FileNotFoundException("源路径不存在!");if (descPath == null || descPath.trim().equals("")) descPath = file.getParent();if (descName == null || descName.trim().equals("")) descName = "";//解压文件unZipDetail(srcPath, descPath, descName);}/** *  * @param srcPath * @param descPath * @param descName * @throws FileNotFoundException * @throws IOException */private static void unZipDetail(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {//获取系统文件编码 (zis , zipFile 不设置charset, 处理中文时可能会抛出 IllegalArgumentException 异常)Charset charset = Charset.forName(System.getProperty("file.encoding"));//获取对应ZipEntry 的输入流ZipFile zipFile = new ZipFile(srcPath, charset);//遍历每一个 ZipEntryZipInputStream zis = new ZipInputStream(new FileInputStream(srcPath), charset);//定义使用的变量int len = 0;String parent = null;ZipEntry entry = null;InputStream is = null;File parentFile = null;String fileName = null;OutputStream os = null;String entryName = null;byte[] bytes = new byte[1024];//遍历处理每一个 ZipEntrywhile((entry = zis.getNextEntry()) != null) {entryName = entry.getName();//构建 ZipEntry所处的目录及文件名if (entryName.contains("\\")) {entryName = entryName.replace("\\", "/");fileName = entryName.substring(entryName.lastIndexOf("/") + 1, entryName.length());parent = entryName.substring(0, entryName.lastIndexOf("/"));if (!descName.equals("")) parent = descName + File.separator + parent;parentFile = new File(descPath + File.separator + parent);} else {fileName = entryName;parentFile = new File(descPath);if (!descName.equals("")) parentFile = new File(parentFile, descName);}//初始化路径、输入流和输出流if (!parentFile.exists()) parentFile.mkdirs();is = zipFile.getInputStream(entry);os = new FileOutputStream(new File(parentFile, fileName));//解压文件while((len = is.read(bytes)) != -1) {os.write(bytes, 0, len);}//关闭资源os.close();is.close();zis.closeEntry();}//关闭资源zis.close();zipFile.close();}}


2.测试类

package foundation.io.zip;import java.io.FileNotFoundException;import java.io.IOException;/** * ZipUtils 测试 *  * @author Administrator * */public class Client {public static void main(String[] args) throws FileNotFoundException, IOException {long start = System.currentTimeMillis();//ZipUtils.enZip("C:/Users/Administrator/Desktop/src", null, null);ZipUtils.unZip("C:/Users/Administrator/Desktop/src.zip", null, "srcTemp");System.out.println("用时:" + (System.currentTimeMillis() - start) + "ms");}}

压缩和解压的是JDK源文件:

压缩用时:4497ms

解压用时:6902ms
类似压缩工具,可以压缩单个文件或者单个目录下的所有文件,没有实现多个文件遍历压缩!


0 0
原创粉丝点击