用Groovy实现解压一个压缩文件,并且找出以数字或者大写字母开头的.groovy文件...

 

用Groovy实现解压一个压缩文件,并且找出解压后文件里面的以数字或者大写字母开头的.groovy文件

 

 

import java.util.regex.Pattern
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

class ZipUtil {

	def static unZip(String path)throws IOException
	{
		def count = -1;
		def index = -1;
		def savepath = "";
		def flag = false;
		def file = null;
		def is = null;
		def fos = null;
		def bos = null;
		
		savepath = "ZIPFolder/";
		ZipFile zipFile = new ZipFile(path);
		Enumeration<?> entries = zipFile.entries();
			
		while(entries.hasMoreElements())
		{
			def buf = new byte[2048];
			ZipEntry entry = (ZipEntry)entries.nextElement();
			def filename = entry.getName();
			
			filename = savepath + filename;
			File file2=new File(filename.substring(0, filename.lastIndexOf("/")));
		
			if(!file2.exists()){
				file2.mkdirs();
			}
							
			if(!filename.endsWith("/")){
				
				file = new File(filename);
				file.createNewFile();
				is = zipFile.getInputStream(entry);
				fos = new FileOutputStream(file);
				bos = new BufferedOutputStream(fos, 2048);
				
				while((count = is.read(buf)) > -1)
				{
					bos.write(buf, 0, count );
				}
				
				bos.flush();
				
				fos.close();
				is.close();

			}
		}
			
		zipFile.close();
			
	}

	def static illegalList=[]
	
	static findIllegalGroovyFile(def filePath){
		
		
		def pattern = Pattern.compile('^([0-9]|[A-Z]).*\\.groovy');
		
		def file=new File(filePath);
		
		def list=file.list()
		
		list.each {
			it ->
			def f=new File(filePath+it)
			if(f.exists()&&!f.isFile()){
				findIllegalGroovyFile(f.getPath()+"/")
			}
			else{
				
					def matcher = pattern.matcher(it);
					if(matcher.find()){
						
						illegalList << f.getPath()
					}
				
			}
			
		}
		
	}
	
	static main(args) {
	
		unZip("ZIPFolder/ZIP.zip")//解压文件
		findIllegalGroovyFile("ZIPFolder/ZIP/")//找出符合题目要求的文件
		illegalList.each{
			it ->
			println it
		}
	}

}

 文件内部结构:

 

最后输出结果:

ZIPFolder\ZIP\777ClosureNum.groovy
ZIPFolder\ZIP\ClosureNum.groovy
ZIPFolder\ZIP\Lottery.groovy
ZIPFolder\ZIP\New folder\1111Lottery.groovy
ZIPFolder\ZIP\New folder\Lottery.groovy
ZIPFolder\ZIP\New folder\LotteryTest.groovy
ZIPFolder\ZIP\New folder (2)\ClosureNum.groovy
ZIPFolder\ZIP\New folder (2)\LotteryTest.groovy
ZIPFolder\ZIP\New folder (2)\New folder\11212LotteryTest.groovy
ZIPFolder\ZIP\New folder (2)\New folder\555rLottery.groovy
ZIPFolder\ZIP\New folder (2)\New folder\ClosureNum.groovy
ZIPFolder\ZIP\New folder (2)\New folder\Lottery.groovy
ZIPFolder\ZIP\New folder (2)\New folder\LotteryTest.groovy

 

 

转载于:https://my.oschina.net/mondchan/blog/73768

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值