Java压缩解压文件/文件夹
来源:互联网 发布:java 食品库存管理 编辑:程序博客网 时间:2024/05/06 03:51
package com.zip;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;/** * 解压缩文件 * * @author baiyang */public class Zip {/** * 压缩文件为zip格式 * * @paramzipFile 压缩文件后的文件名 * @paramsrcPathName 待压缩文件 * @throws IOException * 文件不存在异常 */public static void zipFile(String zipFile, String srcPathName) throws IOException {File file = new File(srcPathName);if (!file.exists())throw new RuntimeException(srcPathName + "not exist!");FileOutputStream fileOutputStream = new FileOutputStream(zipFile + File.separator + srcPathName.substring(srcPathName.lastIndexOf("/")) + ".zip");CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());ZipOutputStream out = new ZipOutputStream(cos);out.setEncoding("GBK");String basedir = "";compress(file, out, basedir);out.flush();out.close();}private static void compress(File file, ZipOutputStream out, String basedir) throws IOException {if (file.isDirectory()) {compressDirectory(file, out, basedir);} else {compressFile(file, out, basedir);}}private static void compressDirectory(File dir, ZipOutputStream out, String basedir) throws IOException {if (!dir.exists())return;File[] files = dir.listFiles();for (int i = 0; i < files.length; i++) {compress(files[i], out, basedir + dir.getName() + "/");}}private static void compressFile(File file, ZipOutputStream out, String basedir) throws IOException {if (!file.exists()) {return;}BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));ZipEntry entry = new ZipEntry(basedir + file.getName());out.putNextEntry(entry);int len = 0;byte data[] = new byte[1024];while ((len = bis.read(data)) != -1) {out.write(data, 0, len);}bis.close();}private static void createDir(String path) {File dir = new File(path);if (dir.exists() == false)dir.mkdir();}private static String getSuffixName(String name) {return name.substring(0, name.lastIndexOf("."));}/** * 解压缩文件 * * @paramzipFilePath 压缩文件路径 * @paramunzipDirectory 要解压到的目录 * @throws IOException * 文件读写异常 */public static void unZip(String zipFilePath, String unzipDirectory) throws IOException {File file = new File(zipFilePath);ZipFile zipFile = new ZipFile(file);File unzipFile = new File(unzipDirectory + "/" + getSuffixName(file.getName()));if (unzipFile.exists())unzipFile.delete();unzipFile.mkdirs();@SuppressWarnings("rawtypes")Enumeration zipEnum = zipFile.getEntries();InputStream input = null;OutputStream output = null;ZipEntry entry = null;while (zipEnum.hasMoreElements()) {entry = (ZipEntry) zipEnum.nextElement();String entryName = new String(entry.getName());String names[] = entryName.split("\\/");int length = names.length;String path = unzipFile.getAbsolutePath();for (int v = 0; v < length; v++) {if (v < length - 1) {path += "/" + names[v] + "/";createDir(path);} else {if (entryName.endsWith("/"))createDir(unzipFile.getAbsolutePath() + "/" + entryName);else {input = zipFile.getInputStream(entry);output = new FileOutputStream(new File(unzipFile.getAbsolutePath() + "/" + entryName));byte[] buffer = new byte[1024 * 8];int readLen = 0;while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)output.write(buffer, 0, readLen);input.close();output.flush();output.close();}}}}}}
为了解决汉字乱码问题,代码中用到了ant
下载所需Ant
- java 压缩、解压文件、文件夹。
- Java压缩解压文件/文件夹
- java压缩及解压(.zip)文件文件夹
- java 解压,压缩文件夹
- java批量压缩/解压文件夹
- java文件夹压缩和解压
- java文件夹压缩和解压
- -Java压缩解压文件、文件夹的一个工具类
- Java 文件或者文件夹的压缩和解压
- JAVA 压缩解压文件
- JAVA文件压缩、解压
- 压缩解压文件--Java
- java压缩,解压文件
- C#压缩解压文件及文件夹Helper
- 解压*UC*.rar文件,压缩文件夹命令
- 解压,压缩 Windows文件 or 文件夹
- 文件文件夹压缩解压工具类
- Java使用I/O压缩解压文件夹
- 2012.6.6文件操作
- 数组与指针
- 网络端口号大全
- PHP 下 显示 完整 错误信息
- C++标准转换运算符reinterpret_cast (转)
- Java压缩解压文件/文件夹
- PHPExcel php execl 下载 或 保存到本地
- 2012.6.6文件操作实例(树型)
- Oracle 内存一 手动内存管理,自动内存管理
- Android 一个apk多个ICON执行入口
- struts2 action初始化参数问题
- DWR购物车范例
- 堆排序
- 16.1 涨工资