Java圧縮ファイル

1387 ワード

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();