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

原创粉丝点击