java 利用7z.exe 压缩解压


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;

public class Sevenzip2 {

static String sevenZipPath = "C:\\Program Files\\7-Zip\\7z.exe"; // 7zip程序的路径
String outDir="out";

public String getOutDir() {
	return outDir;
}

public void setOutDir(String outDir) {
	this.outDir = outDir;
	File dir=new File(outDir);
	dir.mkdirs();
}
/**
 * 
 * @param filename 文件名
 * @param dir 要压缩的文件夹
 */
public void compress(String filepath, String dir) {
	   String filename= Paths.get(filepath).getFileName().toString();
	   String newfile=this.getOutDir()+File.separatorChar+filename;
	   newfile =filepath;
	   System.out.println("压缩到"+newfile);
      ProcessBuilder pb = new ProcessBuilder(sevenZipPath, "a", "-tzip",newfile, dir);

    try {
        Process process = pb.start();
        int exitcode=process.waitFor(); // 等待命令执行完毕
        if(0!=exitcode) {
    		System.out.println("压缩错误");
    		System.exit(0);
    	}else {
    		System.out.println("压缩成功");
    	}
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }

    
}

public static void unzip(String file,String outdir) {
	
}

public static void main1(String[] args) {
    String archiveName = "jggfhiuikgh.rar"; // 压缩文件的名称
    String filePath="2017110214343268.zip";
    String directoryToCompress = "extract1713171736672"; // 要压缩的文件夹路径

   String filename= Paths.get(filePath).getFileName().toString();
    String newfile="out/"+filename;
    
    ProcessBuilder pb = new ProcessBuilder(sevenZipPath, "a", newfile, directoryToCompress);

    try {
        Process process = pb.start();
        int exitcode=process.waitFor(); // 等待命令执行完毕
       	System.out.println("压缩程序返回"+exitcode);
    	if(0!=exitcode) {
    		System.out.println("压缩错误");
    		System.exit(0);
    	}else {
    		System.out.println("压缩成功");
    	}
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

	/**
	 * 返回解压后的绝对路径列表 
	 * @param file
	 * @param outdir
	 * @return
	 */
	public ArrayList<String> extract(String file,String outdir) {
//		setOutDir(outdir);
		ProcessBuilder pb ;
		 Process process;
		 String line;
		 StringBuilder sb=new StringBuilder();
		 pb = new ProcessBuilder(sevenZipPath, "l", file,"-bb2");
		 ArrayList<String> list=new ArrayList<>();
		 try {
			process = pb.start();
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
			boolean a=false;
	    	while ((line = reader.readLine()) != null) {
	    	    System.out.println("程序输出:"+line);
	    		if(a) {
	    			
	    			if(line.length()>5&& "-----".equals(line.substring(0,5))) {
	        			a=false;
	        		}else {
	        			line =line.replaceAll(" +", " ");
	        			 String[] strArr=line.split(" +");
	        			 line.indexOf(' ');
	      	    	    System.out.println(line.indexOf(' '));
	      	    	  strArr[0]=line.substring(0,line.indexOf(' '));
	      	    	  String endstr=line.substring(line.indexOf(' ')+1);
	      	    	 strArr[1]=endstr.substring(0,endstr.indexOf(' '));
	      	    	   endstr=endstr.substring(endstr.indexOf(' ')+1);
	      	    	 strArr[2]=endstr.substring(0,endstr.indexOf(' '));

	      	    	   endstr=endstr.substring(endstr.indexOf(' ')+1);
	      	    	 strArr[3]=endstr.substring(0,endstr.indexOf(' '));
	      	    	 
	   	    	   endstr=endstr.substring(endstr.indexOf(' ')+1);
	      	    	 strArr[4]=endstr.substring(0,endstr.indexOf(' '));
	      	    	 
	      	       endstr=endstr.substring(endstr.indexOf(' ')+1);
	      	    	 strArr[5]=endstr;
	      	    	 
	      	    	    System.out.println(Arrays.toString(strArr));
	      	    	  switch (strArr[2]) {
						case "D....":
	//						System.out.println("is dir");
							break;
						case "....A":
	//						System.out.println("is file");
							list.add(strArr[5]);
							break;
		
						default:
							System.out.println("unkonw type");
							break;
						}
	        		}
	    			
	    		}else {
	    			if(line.length()>5&& "-----".equals(line.substring(0,5))) {
	        			a=true;
	        		}
	    		}
	    	
	    	    sb.append(line+"\n");
	    	}
	    	reader.close();
	//    	 System.out.println("程序所有输出:"+sb);
	   	 System.out.println(list);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		 System.exit(0);;
		 //解压
		pb= new ProcessBuilder(sevenZipPath, "e", file, "-o"+outdir,"*","-bb0");
	    try {
	        process = pb.start();
	        int exitcode=process.waitFor(); // 等待命令执行完毕
	    	BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
	    	
	    	while ((line = reader.readLine()) != null) {
	    	    System.out.println("程序输出:"+line);
	    	}
	    	reader.close();
	    	
	       	System.out.println("压缩程序返回"+exitcode);
	    	if(0!=exitcode) {
	    		System.out.println("解压缩错误");
	    		System.exit(0);
	    	}else {
	    		System.out.println("解压缩成功");
	    	}
	    } catch (IOException | InterruptedException e) {
	        e.printStackTrace();
	    }
	    
	    for (int i = 0; i < list.size(); i++) {
	        list.set(i, outdir+File.separator+ list.get(i)); // 修改元素
	    }
		
		return list;
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值