1. 程式人生 > >壓縮zip檔案和解壓zip格式的檔案

壓縮zip檔案和解壓zip格式的檔案

<span style="white-space:pre">	</span>/**
	*
	*單檔案壓縮
	*
	*/
	@Test
	public void zip() throws IOException{
		String path="D://compressssss//b.zip";
		File file=new File(path.substring(0, path.lastIndexOf("//")));
		//如果不存在這個資料夾就建立這個資料夾
		if(!file.exists()){
			file.mkdirs();
		}
		ZipArchiveOutputStream zipOut=new ZipArchiveOutputStream(new FileOutputStream(path));
		InputStream in=new FileInputStream("E://test.txt");
		ZipArchiveEntry entry=new ZipArchiveEntry("test.txt");
		zipOut.putArchiveEntry(entry);
		
		byte[] b=new byte[1024];
		int len=0;
		while((len=in.read(b))!=-1){
			zipOut.write(b,0,len);
		}
		zipOut.closeArchiveEntry();
		zipOut.close();
		
	}
	
	/**
	 * 多檔案壓縮成zip包
	 * @throws IOException
	 */
	
	@Test
	public void MulFileCompress() throws IOException{
		//壓縮包的指定路徑
		String path="D://zip//code.zip";
		File file2=new File(path.substring(0, path.lastIndexOf("//")));
		//如果不存在這個資料夾就建立這個資料夾
		if(!file2.exists()){
			file2.mkdirs();
		}
		ZipArchiveOutputStream zipOut=new ZipArchiveOutputStream(new FileOutputStream(path));
		//將要進行壓縮的文件"E:\\工作文件"
		File file=new File("E:\\工作文件");
		System.out.println(file.getAbsolutePath());
		String filepath=file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\")+1);
		System.out.println(filepath);
		getFile(file,zipOut,filepath);
		zipOut.close();
		
	}
	
	private void getFile(File file,ZipArchiveOutputStream zipOut,String filepath) throws IOException{
		boolean isDirectory=file.isDirectory();
		//判斷子檔案是否是目錄是的話進行遞迴,不是目錄而是檔案的話,就直接進行壓縮
		if(isDirectory){
			File[] files=file.listFiles();
			for (File f : files) {
				getFile(f,zipOut,filepath);
			}
		}else{
			InputStream in=new FileInputStream(file.getAbsolutePath());
			if(file.getAbsolutePath()!="D://"){
			String path=file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(filepath)+filepath.length());
			System.out.println(path);
			ZipArchiveEntry entry=new ZipArchiveEntry(path);
			zipOut.putArchiveEntry(entry);
			byte[] b=new byte[1024];
			int len=0;
			while((len=in.read(b))!=-1){
				zipOut.write(b,0,len);
			}
			zipOut.closeArchiveEntry();
			}
		}
	}
	
	/**
	 * 解壓zip檔案
	 * @throws IOException
	 */
	
	@Test
	public void Test() throws IOException{
		File file=new File("D://zip//code.zip");
		unCompress(file,"D://unzip//");
	}
	
	public void unCompress(File zipFile,String descDir)throws IOException{  
        File pathFile = new File(descDir);  
        if(!pathFile.exists()){  
            pathFile.mkdirs();  
        }  
        ZipFile zip = new ZipFile(zipFile);  
        for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){  
            ZipArchiveEntry entry = (ZipArchiveEntry)entries.nextElement();  
            String zipEntryName = entry.getName();  
            InputStream in = zip.getInputStream(entry);  
            String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");
            //判斷路徑是否存在
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));  
            if(!file.exists()){  
                file.mkdirs();  
            }  
            //判斷檔案全路徑是否為資料夾 
            if(new File(outPath).isDirectory()){  
                continue;  
            }  
            OutputStream out = new FileOutputStream(outPath);  
            byte[] buf1 = new byte[1024];  
            int len;  
            while((len=in.read(buf1))>0){  
                out.write(buf1,0,len);  
            }  
            in.close();  
            out.close();  
            }  
    }