發個文件壓縮解壓的代碼吧。

我是代碼派
public class ZipUtils {

	/**
	 * 解壓文件
	 * @param zipPath
	 * @param toPath
	 * @throws IOException
	 */
	public static void unCompress(String zipPath, String toPath) throws IOException {
		if(!toPath.endsWith("\\"))toPath += "\\";
		File destFile = new File(toPath);
		if(!destFile.exists())destFile.mkdirs();
		File zipfile = new File(zipPath);
		ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
		ZipEntry entry = null;
		while ((entry = zis.getNextEntry()) != null) {
			if (entry.isDirectory()) {
				File file = new File(toPath + entry.getName() + "\\");
				file.mkdirs();
			} else {
				File file = new File(toPath + entry.getName());
				if(!file.getParentFile().exists())file.getParentFile().mkdirs();
				FileOutputStream fos = new FileOutputStream(file);
				byte buf[] = new byte[1024];
				int len = -1;
				while ((len = zis.read(buf, 0, 1024)) != -1) {
					fos.write(buf, 0, len);
				}
				fos.close();
			}
		}
		zis.close();
	}
	
	public static byte[] getData(String zipPath, String path) throws IOException{
		File zipfile = new File(zipPath);
		ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ZipEntry entry = null;
		while ((entry = zis.getNextEntry()) != null) {
			if(entry.getName().equals(path)){
				byte buf[] = new byte[1024];
				int len = -1;
				while ((len = zis.read(buf, 0, 1024)) != -1) {
					baos.write(buf, 0, len);
				}
				break;
			}
		}
		baos.close();
		zis.close();
		return baos.toByteArray();
	}

	/**
	 * 壓縮單個文件
	 * @param file
	 */
	public static void compress(File file) {
		try {
			String fileName = file.getName();
			if(fileName.indexOf(".") != -1)
				fileName = fileName.substring(0, fileName.indexOf("."));
			FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName+ ".zip");
			CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
			InputStream in = new FileInputStream(file);
			out.putNextEntry(new ZipEntry(file.getName()));
			int len = -1;
			byte buf[] = new byte[1024];
			while((len = in.read(buf, 0, 1024)) != -1)
				out.write(buf, 0, len);
			out.closeEntry();
			
			in.close();
			out.close();
		} catch (Exception e) {
			System.err.println(e);
		}
	}

	/**
	 * 壓縮文件夾
	 * @param file
	 * @throws IOException
	 */
	public static void compressDir(File file) throws IOException{
		FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
		CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
		ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
		
		compressDir(file, out, file.getAbsolutePath());
		
		out.flush();
		out.close();
	}
	
	/**
	 * 壓縮文件夾遞歸調用方法
	 * @param file
	 * @param out
	 * @throws IOException
	 */
	private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException{
		if(srcFile.isDirectory()){
			File subfile[] = srcFile.listFiles();
			for(int i=0; i< subfile.length; i++){
				compressDir(subfile[i], out, destPath);
			}
		}else{
			InputStream in = new FileInputStream(srcFile);
			String name = srcFile.getAbsolutePath().replace(destPath, "");
			if(name.startsWith("\\"))name = name.substring(1);
			ZipEntry entry = new ZipEntry(name);
			entry.setSize(srcFile.length());
			entry.setTime(srcFile.lastModified());
			out.putNextEntry(entry);
			int len = -1;
			byte buf[] = new byte[1024];
			while((len = in.read(buf, 0, 1024)) != -1)
				out.write(buf, 0, len);
			
			out.closeEntry();
			in.close();
		}
	}
	
	public static void main(String[] args) throws Exception {
		ZipUtils.compress(new File("d:/aa/a01.jpg"));

//		try {
//			byte buf[] = ZipDemo.getData("d:/aa.zip", "aa.txt");
//			System.out.println(buf.length);
//			ZipDemo.unCompress("d:/aa.zip", "d:/kk/");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

 

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 4901
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章