package com.lss.common.file.zip; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * @description 压缩文件 * @author SHOUSHEN LUAN * @DATE 2012-1-8下午03:10:01 */ public class GZipUtils { public static void main(String[] args) throws IOException { zipFile("D:\\test\\kevin.mp4", "D:\\test\\kevin.mp4.zip"); unZipFile("D:\\test\\kevin.mp4.zip", "D:\\test\\kevin.mp4.zip.mp4"); } /** * Member cache 文件解压处理 * * @param buf * @return * @throws IOException */ public static byte[] unGzip(byte[] buf) throws IOException { GZIPInputStream gzi = null; ByteArrayOutputStream bos = null; try { gzi = new GZIPInputStream(new ByteArrayInputStream(buf)); bos = new ByteArrayOutputStream(buf.length); int count = 0; byte[] tmp = new byte[2048]; while ((count = gzi.read(tmp)) != -1) { bos.write(tmp, 0, count); } buf = bos.toByteArray(); } finally { if (bos != null) { bos.flush(); bos.close(); } if (gzi != null) gzi.close(); } return buf; } /** * Member cache 文件压缩处理 * * @param val * @return * @throws IOException */ public static byte[] gzip(byte[] val) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length); GZIPOutputStream gos = null; try { gos = new GZIPOutputStream(bos); gos.write(val, 0, val.length); gos.finish(); gos.flush(); bos.flush(); val = bos.toByteArray(); } finally { if (gos != null) gos.close(); if (bos != null) bos.close(); } return val; } /** * 对文件进行压缩 * * @param source * 源文件 * @param target * 目标文件 * @throws IOException */ public static void zipFile(String source, String target) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; GZIPOutputStream gzout = null; try { fin = new FileInputStream(source); fout = new FileOutputStream(target); gzout = new GZIPOutputStream(fout); byte[] buf = new byte[1024]; int num; while ((num = fin.read(buf)) != -1) { gzout.write(buf, 0, num); } } finally { if (gzout != null) gzout.close(); if (fout != null) fout.close(); if (fin != null) fin.close(); } } /** * 解压文件 * * @param source源文件 * @param target目标文件 * @throws IOException */ public static void unZipFile(String source, String target) throws IOException { FileInputStream fin = null; GZIPInputStream gzin = null; FileOutputStream fout = null; try { fin = new FileInputStream(source); gzin = new GZIPInputStream(fin); fout = new FileOutputStream(target); byte[] buf = new byte[1024]; int num; while ((num = gzin.read(buf, 0, buf.length)) != -1) { fout.write(buf, 0, num); } } finally { if (fout != null) fout.close(); if (gzin != null) gzin.close(); if (fin != null) fin.close(); } } } package com.lss.common.file.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; @SuppressWarnings("unchecked") public class ZipToFile { public static final String ZIP_FILENAME = "D:\\test\\source.zip";// 需要解压缩的文件名 public static final String ZIP_DIR = "D:\\test\\source\\";// 需要压缩的文件夹 public static final String UN_ZIP_DIR = "D:\\test\\";// 要解压的文件目录 public static final int BUFFER = 1024;// 缓存大小 public static void main(String[] args) { try { // zipFile(ZIP_DIR,ZIP_FILENAME); unZipFile(); } catch (Exception e) { e.printStackTrace(); } } /** * zip压缩功能. 压缩baseDir(文件夹目录)下所有文件,包括子目录 * * @throws Exception */ public static void zipFile(String baseDir, String fileName) throws Exception { List fileList = getSubFiles(new File(baseDir)); ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(fileName)); ZipEntry ze = null; byte[] buf = new byte[BUFFER]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(baseDir, f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, BUFFER)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); } /** * 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径. * * @param baseDir * java.lang.String 根目录 * @param realFileName * java.io.File 实际的文件名 * @return 相对文件名 */ private static String getAbsFileName(String baseDir, File realFileName) { File real = realFileName; File base = new File(baseDir); String ret = real.getName(); while (true) { real = real.getParentFile(); if (real == null) break; if (real.equals(base)) break; else ret = real.getName() + "/" + ret; } return ret; } /** * 取得指定目录下的所有文件列表,包括子目录. * * @param baseDir * File 指定的目录 * @return 包含java.io.File的List */ private static List getSubFiles(File baseDir) { List ret = new ArrayList(); File[] tmp = baseDir.listFiles(); for (int i = 0; i < tmp.length; i++) { if (tmp[i].isFile()) ret.add(tmp[i]); if (tmp[i].isDirectory()) ret.addAll(getSubFiles(tmp[i])); } return ret; } /** * 解压缩功能. 将ZIP_FILENAME文件解压到ZIP_DIR目录下. * * @throws Exception */ public static void unZipFile() throws Exception { ZipFile zfile = new ZipFile(ZIP_FILENAME); Enumeration zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { File f = new File(ZIP_DIR + ze.getName()); f.mkdir(); continue; } OutputStream os = new BufferedOutputStream(new FileOutputStream( getRealFileName(ZIP_DIR, ze.getName()))); InputStream is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } is.close(); os.close(); } zfile.close(); } /** * 给定根目录,返回一个相对路径所对应的实际文件名. * * @param baseDir * 指定根目录 * @param absFileName * 相对路径名,来自于ZipEntry中的name * @return java.io.File 实际的文件 */ public static File getRealFileName(String baseDir, String absFileName) { String[] dirs = absFileName.split("/"); File ret = new File(baseDir); if (dirs.length > 1) { for (int i = 0; i < dirs.length - 1; i++) { ret = new File(ret, dirs[i]); } if (!ret.exists()) ret.mkdirs(); ret = new File(ret, dirs[dirs.length - 1]); return ret; } return ret; } }