用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
		}
	}

}

 文件内部结构:

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

 

最后输出结果:

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

 

 

你可能感兴趣的:(java,groovy)