java写的zip压缩和解压缩程序

java写的zip压缩程序

/**
 * @author HJX
 * @version 1.0,2014-01-14
 * @since JDK1.7
 * java写的zip压缩程序
 * ZipCompress
 * ZipCompress-main
 * ZipCompress-compressFileHelper
 * ZipCompress-compressFile
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//apache的zip可以防止出现中文乱码的情况
//java的zip则会出现中文乱码
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipCompress {

	/**
	 * @param inputFileName [outputFileName]
	 * @return void
	 */
	public static void main(String[] args) {
		// 判断参数合法性
		if ((args.length == 0) || (args.length > 2)) {
			System.err.println("Usage:CompressTest inputFileName [outputFileName]");
			System.exit(-1);
		}

		// 开始压缩的时刻
		double startTime = System.currentTimeMillis();
		ZipCompress compressTest = new ZipCompress();
		// 被压缩目标的路径,可以指向一个文件或者一个目录
		String srcFile = args[0];
		// 生成的压缩文件
		String destFile = "";
		if (args.length == 2) {
			destFile = args[1];
		} else {
			destFile = srcFile + ".zip";
		}
		// 相对地址,基地址,根目录
		String basePath = "";
		ZipOutputStream zos = null;

		try {
			// 创建一个zip输出流
			zos = new ZipOutputStream(new FileOutputStream(destFile));
			// 启动压缩进程
			compressTest.compressFileHelper(zos, basePath, srcFile);
			// 压缩成功的时刻
			double endTime = System.currentTimeMillis();
			System.out.println("压缩成功!耗时" + (endTime - startTime) / 1000.0 + "s");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (zos != null)
					zos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void compressFileHelper(ZipOutputStream zos, String basePath,
			String srcFile) {
		File file = new File(srcFile);
		if (file.isDirectory()) {
			// 如果是目录
			try {
				zos.putNextEntry(new ZipEntry(basePath + file.getName() + "/"));
				zos.closeEntry();
			} catch (IOException e) {
				e.printStackTrace();
			}
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++)
				compressFileHelper(zos, basePath + file.getName() + "/",
						files[i].getPath());
		} else {
			compressFile(zos, basePath, file);
		}
	}

	public void compressFile(ZipOutputStream zos, String basePath, File srcFile) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(srcFile);
			zos.putNextEntry(new ZipEntry(basePath + srcFile.getName()));
			int length = 0;
			int bufferSize = 1024;
			byte[] butter = new byte[bufferSize];
			while ((length = fis.read(butter, 0, bufferSize)) >= 0) {
				zos.write(butter, 0, length);
			}
			zos.closeEntry();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}



java写的zip解压缩程序

/**
 * @author HJX
 * @version 1.0,2014-01-14
 * @since JDK1.7
 * java写的zip解压缩程序
 * ZipDecompress
 * ZipDecompress-main
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipDecompress {

	/**
	 * @param inputFileName [outputFileName]
	 * @return null
	 */
	public static void main(String[] args) {
		// 判断参数合法性
		if ((args.length == 0) || (args.length > 2)) {
			System.err.println("Usage:CompressTest inputFileName [outputFileName]");
			System.exit(-1);
		}

		// 开始压缩的时刻
		double startTime = System.currentTimeMillis();

		ZipDecompress decompressTest = new ZipDecompress();
		// 被解压缩的压缩文件的路径
		String srcFile = args[0];
		// 输出路径
		String destFile = "";
		if (args.length == 2) {
			destFile = args[1];
		} else {
			destFile = srcFile.substring(0, srcFile.length() - 4);
		}

		ZipInputStream zis = null;
		try {
			// 创建一个zip输入流
			zis = new ZipInputStream(new FileInputStream(args[0]));
			// 输出文件
			File file = null;
			ZipEntry entry;
			int length = 0;
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			FileOutputStream fos = null;
			try {
				while ((entry = zis.getNextEntry()) != null) {
					file = new File(destFile, entry.getName());
					if (!entry.isDirectory()) {
						if (!file.exists()) {
							(new File(file.getParent())).mkdirs();
						}
						fos = new FileOutputStream(file);
						while ((length = zis.read(buffer, 0, bufferSize)) >= 0) {
							fos.write(buffer, 0, length);
						}
					} else {
						file.mkdirs();
					}
				}
				zis.closeEntry();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (fos != null)
						fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			double endTime = System.currentTimeMillis();
			System.out.println("解压成功!耗时:" + (endTime - startTime) / 1000.0 + "s");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (zis != null)
					zis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值