栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java压缩文件(夹)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java压缩文件(夹)

包含压缩文件或文件夹、压缩多个文件或文件夹、按数量分段压缩文件夹
使用带缓冲的输入流提高压缩速度

package com.client.util;

 
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 

public class FileZip {
 
	private static void zip(ZipOutputStream out, File f, String base) throws Exception {
		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));
			BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));
			byte[] b=new byte[1024];
			int len=0;
			while ((len=in.read(b)) != -1) {
				out.write(b,0,len);
			}
			in.close();
		}
	}
	
	public static void zipDir(String dirPath,String zipName){
		File file=new File(dirPath);
		if(file.isDirectory()){
			File[] files = file.listFiles();
		    FileUtil.createIfNoExist( dirPath+"\"+zipName,true);
			try(ZipOutputStream zipOutputStream = new ZipOutputStream(
					new FileOutputStream(dirPath + "\" + zipName))
			){
				zip(zipOutputStream,files,"");
			    zipOutputStream.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void zip(ZipOutputStream out,File[] files,String base) throws Exception {
		for (int i = 0; i < files.length; i++) {
			zip(out, files[i], base +files[i].getName());
		}
	}
	
    public static void zip(String zipPathname,File[] files){
		FileUtil.createIfNoExist(zipPathname,true);
		try(ZipOutputStream zipOutputStream = new ZipOutputStream(
				new FileOutputStream(zipPathname))
		){
			zip(zipOutputStream,files,"");
			zipOutputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void partitionZipDir(String dirPath,int itemsPerZip){
		File dir=new File(dirPath);
		if(!dir.isDirectory())throw new RuntimeException(dirPath+" 不是目录");
		File[] files = dir.listFiles();
	    int total= files.length/itemsPerZip;
		if(files.length%itemsPerZip!=0)total++;
		System.out.println("压缩文件数:"+total);
		int zipCount=1;
		int start,end;
		while (true){
			System.out.println("("+zipCount+"/"+total+")");
			start=(zipCount-1)*itemsPerZip;
			end=zipCount*itemsPerZip-1;
			if(end>= files.length)end= files.length-1;
			   File[] partFiles=new File[end-start+1];
			System.arraycopy(files, start, partFiles, 0, partFiles.length);
			zip(dirPath+"/zipPart"+zipCount+".zip",
					partFiles);
			if(end>= files.length-1)break;
		    zipCount++;
		}
	}
	
	public static void zipFile(String filePathname,String zipPathname){
		File file=new File(filePathname);
		if(file.isFile()){
			FileUtil.createIfNoExist( zipPathname,true);
			try(ZipOutputStream zipOutputStream = new ZipOutputStream(
					new FileOutputStream(zipPathname))
			){
				zip(zipOutputStream,file,"");
				zipOutputStream.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String [] temp){
		partitionZipDir("E:\a",10);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号