java压缩、解压Zip

package test;

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

public class TestZip {
	public void zip(String zipFileName, String inputFile) throws Exception {
		zip(zipFileName, new File(inputFile));
	}

	public void zip(String zipFileName, File inputFile) throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		zip(out, inputFile, "");
		System.out.println("zip done");
		out.close();
	}

	public void unzip(String zipFileName, String outputDirectory)
			throws Exception {
		ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
		ZipEntry z;
		while ((z = in.getNextEntry()) != null) {
			System.out.println("unziping " + z.getName());
			if (z.isDirectory()) {
				String name = z.getName();
				name = name.substring(0, name.length() - 1);
				File f = new File(outputDirectory + File.separator + name);
				f.mkdir();
				System.out.println("mkdir " + outputDirectory + File.separator
						+ name);
			} else {
				File f = new File(outputDirectory + File.separator
						+ z.getName());
				f.createNewFile();
				FileOutputStream out = new FileOutputStream(f);
				int b;
				while ((b = in.read()) != -1)
					out.write(b);
				out.close();
			}
		}

		in.close();
	}

	public void zip(ZipOutputStream out, File f, String base) throws Exception {
		System.out.println("Zipping " + f.getName());
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			out.putNextEntry(new ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName());
			}
		} else {
			out.putNextEntry(new ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int b;
			while ((b = in.read()) != -1)
				out.write(b);
			in.close();
		}

	}

	public static void main(String[] args) {
		try {
			TestZip t = new TestZip();
			t.zip("e:\\test.zip", "D:\\images\\A100000000230");
			// t.unzip("e:\\test.zip", "e:\\test2");
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
}

 

package test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class OperFile {

	public void zipFold(File unzipf, File zipf) throws Exception {// unzipf是待压缩的文件,zipf是压缩后的文件
		FileOutputStream fos;
		CheckedOutputStream csum;
		ZipOutputStream out;// 新定义的
		int index;
		if (!unzipf.exists())
			return;
		else {
			fos = new FileOutputStream(zipf);
			csum = new CheckedOutputStream(fos, new Adler32());
			out = new ZipOutputStream(new BufferedOutputStream(csum));
			index = unzipf.toString().lastIndexOf("\\");
			zipF(unzipf, out, index);
			out.close();
		}

		return;
	}

	public void zipF(File f, ZipOutputStream out, int index) throws Exception {
		if (f.isFile()) {
			FileInputStream in = new FileInputStream(f);
			String etr = f.toString().substring(index + 1);
			out.putNextEntry(new ZipEntry(etr));
			byte[] b = new byte[4096];
			int byte_read;
			while ((byte_read = in.read(b)) != -1)
				out.write(b, 0, byte_read);
			in.close();
			return;

		} else {
			ZipEntry ze = new ZipEntry(f.toString().substring(index + 1) + "/");
			out.putNextEntry(ze);
			File[] fs = f.listFiles();
			if (fs != null)
				for (int i = 0; i < fs.length; i++)
					zipF(fs[i], out, index);
		}
	}// zipF()定义完毕

	public static void main(String args[]) {
		OperFile o = new OperFile();
		try {
			o.zipFold(new File("D:\\images"), new File("E:\\test.zip"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(java,F#,idea)