Java压缩文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

	private static int BUF = 1024 * 8;

	private static String folderPath = "F:\\压缩";
	private static String srcFileName = "log";
	private static String zipFileName = "log.zip";

		File srcFile = new File(folderPath + File.separator + srcFileName);
		File zipFile = new File(folderPath + File.separator + zipFileName);

		InputStream is = new FileInputStream(srcFile);
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
		zos.setComment("zip comment"); // 设置注释

		long startTime = System.currentTimeMillis();

		zos.putNextEntry(new ZipEntry(srcFileName));

		byte[] buf = new byte[BUF];
		int len;
		while ((len = is.read(buf)) != -1) {
			zos.write(buf, 0, len);
		}

		long endTime = System.currentTimeMillis();
		long cost = endTime - startTime;
		System.out.println("压缩耗时:" + cost + "毫秒");

		if (is != null)
			is.close();
		if (zos != null)
			zos.close();

你可能感兴趣的:(Java压缩文件)