1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public String logPath;
public String archivePathAndName;
public Zip()
{
logPath = "D:\\test1\\";
archivePathAndName = "D:\\test1\\";
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter=
new SimpleDateFormat("yyyyMMddHHmmss");
String dateNow = formatter.format(currentDate.getTime());
System.out.println("Now the date is :=> " + dateNow);
archivePathAndName = archivePathAndName+"Archive_Logs_"+dateNow+".zip";
}
public int MakeZip()
{
try {
// get a list of files from log directory
File f = new File(this.logPath);
String files[] = f.list();
for (int i=0; i<files.length; i++) {
files[i] = this.logPath + files[i];
}
BufferedInputStream origin = null;
// Creation of a output stream to a file, this file will be the Zip archive final
FileOutputStream dest = new FileOutputStream(archivePathAndName);
// Creation of a output stream Zip to this file through the buffer
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
// Specify the method of compression choosen
out.setMethod(ZipOutputStream.DEFLATED);
// Specify the compression level (int between 0 and 9)
out.setLevel(9);
// Buffer size for in and out buffer
byte data[] = new byte[BUFFER];
for (int i=0; i<files.length; i++) {
// LogFiles logFiles = new LogFiles(files[i]);
// if (logFiles.getFilenameOfLogfileAt(i) != "") {
Pattern p = Pattern.compile("sys(.)+201003(.)+?6\\d");
Matcher m = p.matcher(files[i]);
boolean b = m.matches();
f = new File(files[i]);
if ((f.isDirectory()==false) && (m.find())) {
System.out.println("Adding: "+files[i]);
// the file
FileInputStream fi = new FileInputStream(files[i]);
// In buffer
origin = new BufferedInputStream(fi, BUFFER);
// Zip Entry for each
ZipEntry entry = new ZipEntry(files[i]);
// Affect it to in stream
out.putNextEntry(entry);
// Écriture des entrées dans le flux de sortie par paquet de taille
// égale aux tampons dentrée et de sortie
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
// close current entry
origin.close();
}
else {
System.out.println("This is a directory and won't be zipped: "+files[i]);
}
}
// close stream
out.close();
/*
// delete all the files
for (int i=0; i<files.length; i++) {
File fileToDelete = new File(files[i]);
fileToDelete.delete();
}
*/
} catch(Exception e) {
e.printStackTrace();
return 0;
}
return 1;
}
public static void main (String argv[]) {
Zip test = new Zip();
test.MakeZip();
}
} |
Partager