Java实现文件压缩 使用GZIP和Zip方式http://blog.csdn.net/kevin_luan/article/details/7903400
来源:互联网 发布:苹果软件视频桌面 编辑:程序博客网 时间:2024/05/17 03:32
- 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;
- }
- }
1 0
- Java实现文件压缩 使用GZIP和Zip方式http://blog.csdn.net/kevin_luan/article/details/7903400
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- gzip压缩算法 http://blog.csdn.net/hguisu/article/details/7795435
- Java实现文件压缩 使用GZIP和Zip方式
- java http://blog.csdn.net/wike163/article/details/6635321
- java socket文件上传 转之 http://blog.csdn.net/huang930528/article/details/52401565
- /etc/profile和 . profile 文件 http://blog.csdn.net/louiswang2009/article/details/5886575
- 使用dup和dup2重定向和还原 http://blog.csdn.net/kangear/article/details/24098315
- 使用信号实现异步通知机制的例子 http://blog.csdn.net/buaa_shang/article/details/9103155
- java实现站内搜索转自http://blog.csdn.net/liunian02050328/article/details/8220379
- java中接口的定义与实现 http://blog.csdn.net/yjkwf/article/details/7238847
- java spring为ApplicationContext提供有三种实现http://blog.csdn.net/jyonghu003/article/details/59631621
- xmlInitParser和xmlCleanupParser使用详解 http://blog.csdn.net/hdutigerkin/article/details/7548596
- C++中智能指针的设计和使用 http://blog.csdn.net/hackbuteer1/article/details/7561235
- CreateThread和_beginthread区别及使用http://blog.csdn.net/wxq1987525/article/details/6620210
- 类模板的定义和使用 http://blog.csdn.net/djinglan/article/details/6689839
- Java程序和C程序的相互调用http://blog.csdn.net/yasi_xi/article/details/8437628
- dojo EnhancedGrid的两种实现方式对比,转载自http://blog.csdn.net/earthhour/article/details/17203515
- 黑马程序员———面向对象(一)
- 修改Java文件不用重启Tomcat服务的方法
- 基于数组二分查找算法的实现
- C++内存管理学习
- 《More Effective C++》重点摘要二:操作符
- Java实现文件压缩 使用GZIP和Zip方式http://blog.csdn.net/kevin_luan/article/details/7903400
- zoj 3876 May Day Holiday
- tiled使用四大模块
- Unexpected exception of type [PDOException][HY000] [2005] Unknown MySQL server host
- 练习场ACM 第845题
- freeMarker 命名空间
- Android禁止ViewPager的左右滑动
- JavaWeb中级
- [LeetCode]Pow(x, n)