java解压zip和gz文件实例

static public boolean unZip(File unZipfileName){
		int BUFFER=1024;
		 try {
	 
	            String filePath = unZipfileName.getParent()+"/";
	            ZipFile zipFile = new ZipFile(unZipfileName);
	            Enumeration emu = zipFile.entries();
	            int i=0;
	            while(emu.hasMoreElements()){
	                ZipEntry entry = (ZipEntry)emu.nextElement();
	             
	                if (entry.isDirectory())
	                {
	                    new File(filePath + entry.getName()).mkdirs();
	                    continue;
	                }
	                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
	                File file = new File(filePath + entry.getName());
	                File parent = file.getParentFile();
	                if(parent != null && (!parent.exists())){
	                    parent.mkdirs();
	                }
	                FileOutputStream fos = new FileOutputStream(file);
	                BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);           
	                
	                int count;
	                byte data[] = new byte[BUFFER];
	                while ((count = bis.read(data, 0, BUFFER)) != -1)
	                {
	                    bos.write(data, 0, count);
	                }
	                bos.flush();
	                bos.close();
	                bis.close();
	            }
	            zipFile.close();
	            return true;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return false;
	        }
    }  
	

 解压GZ文件:

public static boolean unGzFile(File unGzipfileName){
		String filePath = unGzipfileName.getParent();
		try {
            GZIPInputStream gis = new GZIPInputStream(new FileInputStream(unGzipfileName));  
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath+"/farfetch.xml"));
			byte[] buffer = new byte[1024 * 8]; 
			int count = 0; 
			while((count =gis.read(buffer)) != -1){
				bos.write(buffer, 0, count);
			}
			bos.flush();
			bos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

猜你喜欢

转载自sky-xin.iteye.com/blog/2263000
今日推荐