import groovy.transform.Field
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream
class FileEntry{
String filename;
String dir;
Date date;
String day;
String time;
DateFormat dayDf = new SimpleDateFormat("yyyy-MM-dd");
DateFormat timeDf = new SimpleDateFormat("HH:mm:ss");
FileEntry(String filename, Date d){
this(filename, null, d);
}
FileEntry(String filename, String dir, Date d){
this.filename = filename;
this.date=d;
day = dayDf.format(d);
time = timeDf.format(d);
}
public String toString(){
return filename+","+day+","+time+"\n";
}
}
String zipFileName = "c:/temp/dropdirsAug2018.zip"
if(args.length>0)
zipFileName=args[0]
ZipFile inputZip
try{
inputZip = new ZipFile(zipFileName)
}catch(Exception e){
println "Cannot find expected file $zipFileName"
return
}
/*
* Table listing files to check
* inName is the path and name of input files (including asterisk wildcard)
* outName is the output name.
*/
def zipFilesToMove = [
[inName:"/path/*filename*.csv", outName:"<#>.filename.csv"],
//[inName:"/path/otherfile.xml", outName:"otherfile.xml", outDir:"/notUsedHere"]
]
def list = doUnzip(inputZip, zipFilesToMove)
//Clean up old folders
//println list
writeToFile(list)
println "Done"
void writeToFile(List
println "Writing to file"
File f = new File("output.csv");
f.text = "";
int x=0;
for(FileEntry line:fileEntry){
f << line.toString();
println line
x++;
}
println "Written $x lines"
}
// Do unzip for specified files
public List
List
//Loop through bigZip to find files we want.
// if we find a file we unzip it based ondate to different folder
Enumeration e = inputZip.entries()
while(e.hasMoreElements()) {
ZipEntry zipEntry = e.nextElement()
if (zipEntry.isDirectory()) {
continue;
}
FileEntry fileEntry = checkIfValidZipEntry(zipEntry, zipFilesToCheck)
if(fileEntry)
ret << fileEntry;
}
return ret
}
public FileEntry checkIfValidZipEntry(ZipEntry zipEntry, List zipFilesToCheck){
String zipEntryName = zipEntry.getName();
long timestamp = zipEntry.getTime()
for(Map zipFileToCheck: zipFilesToCheck){
String regex = getRegex(zipFileToCheck.inName)
// Note String matches, checks if whole string matches, not substring. ? Must use this instead
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(zipEntryName);
boolean match = m.find()
if(match){
Date d = new Date();
d.setTime(zipEntry.getTime())
return new FileEntry(zipFileToCheck.inName, d);
}
}
//println "No match for $zipEntryName.. Ignoring"
return null
}
String getRegex(String filename){
return filename.replaceAll("\\*", ".*").replaceAll("\\/","\\\\/")
}
No comments:
Post a Comment