1. ホーム
  2. Java

Zipファイルの圧縮・解凍にantを使用する

2022-02-19 23:25:32

javaAntを使ってZipファイルの圧縮と解凍を実装する。

単一ファイルのZipファイルへの圧縮、複数ファイルのZipファイルへの圧縮。

大容量ファイルの圧縮が可能。

ant.jarファイルを使用する必要があるので、libディレクトリに置いてください。

ant.jarをダウンロードします。 http://download.csdn.net/detail/xinxin19881112/8906157


Zip.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;

ZipEntry; /**
 * ZIP manipulation tool class
 */
public class Zip {
	
	/**
	 * Compress the list of files to a ZIP file
	 * @param zipFilename ZIP file to be compressed to
	 * @param paths list of files, multiple parameters
	 * @throws Exception
	 */
	public static void compress(String zipFilename, String... paths)
			throws Exception {
		compress(new FileOutputStream(zipFilename), paths);
	}

	/**
	 * Compress the list of files to the output stream
	 * @param os the stream to compress to
	 * @param paths List of files, multiple parameters
	 * @throws Exception
	 */
	public static void compress(OutputStream os, String... paths)
			throws Exception {
		ZipOutputStream zos = new ZipOutputStream(os);
		for (String path : paths) {
			if (path.equals(""))
				continue;
			File file = new java.io;
			if (file.exists()) {
				if (file.isDirectory()) {
					zipDirectory(zos, file.getPath(), file.getName()
							+ File.separator);
				} else {
					zipFile(zos, file.getPath(), "");
				}
			}
		}
		zos.close();
	}
	
	private static void zipDirectory(ZipOutputStream zos, String dirName,
			String basePath) throws Exception {
		File dir = new File(dirName);
		if (dir.exists()) {
			File files[] = dir.listFiles();
			if (files.length > 0) {
				for (File file : files) {
					if (file.isDirectory()) {
						zipDirectory(zos, file.getPath(), basePath
								+ file.getName().substring(
										file.getName().lastIndexOf(
												File.separator) + 1)
								+ File.separator);
					} else
						zipFile(zos, file.getPath(), basePath);
				}
			} else {
				ZipEntry ze = new ZipEntry(basePath);
				zos.putNextEntry(ze);
			}
		}
	}

	private static void zipFile(ZipOutputStream zos, String filename,
			String basePath) throws Exception {
		File file = new File(filename);
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(filename);
			ZipEntry ze = new ZipEntry(basePath + file.getName());
			zos.putNextEntry(ze);
			byte[] buffer = new byte[8192];
			int count = 0;
			while ((count = fis.read(buffer)) > 0) {
				zos.write(buffer, 0, count);
			}
			fis.close();
		}
	}
}












TestCompress.java

import java.io.FileOutputStream;

import com.xx.commmon.Zip;

/**
 * Compressed file test
 */
public class TestCompress {

	public static void main(String[] args) {
		// List of files to be compressed
		String path01 = "E:\\\download\\cn_windows_8_x86_dvd_915414.iso";
		String path02 = "e:\\\PPT Template_V0.1.potx";
		try {
			FileOutputStream os = new FileOutputStream("e:\\\test.zip"); // output ZIP file stream
			Zip.compress(os, path01);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}












TestEctractZip.java

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

/**
 * Decompression test
 */
public class TestEctractZip {

	@SuppressWarnings( { "unchecked", "static-access" })
	public static void main(String[] args) {
		TestEctractZip z = new TestEctractZip();
		ArrayList<String> a = z.Ectract("C:\\a.zip", "C:\tmp\\\\"); // return the list of files extracted
		for(String s : a){
			System.out.println(s);
		}
	}

	/**
	 * Decompress
	 * @param sZipPathFile The file to decompress
	 * @param sDestPath Unzip to a folder
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList Ectract(String sZipPathFile, String sDestPath) {
		ArrayList<String> allFileName = new ArrayList<String>();
		try {
			// first specify the location and file name of the compressed file and create a FileInputStream object
			FileInputStream fins = new FileInputStream(sZipPathFile);
			// Pass fins into the ZipInputStream
			ZipInputStream zins = new ZipInputStream(fins);
			ZipEntry ze = null;
			byte[] ch = new byte[256];
			while ((ze = zins.getNextEntry()) ! = null) {
				File zfile = new File(sDestPath + ze.getName());
				//File zfile = new File(sDestPath +File.separator+ ze.getName());
				File fpath = new File(zfile.getParentFile().getPath());
				if (ze.isDirectory()) {
					if (!zfile.exists())
						zfile.mkdirs();
					zins.closeEntry();
				} else {
					if (!fpath.exists())
						fpath.mkdirs();
					FileOutputStream fouts = new FileOutputStream(zfile);
					int i;
					allFileName.add(zfile.getAbsolutePath());
					while ((i = zins.read(ch)) ! = -1)
						fouts.write(ch, 0, i);
					zins.closeEntry();
					fouts.close();
				}
			}
			fins.close();
			zins.close();
		} catch (Exception e) {
			System.err.println("Extract error:" + e.getMessage());
		}
		return allFileName;
	}
}






New New: http://blog.csdn.net/xinxin19881112/article/details/46913931