Import org.apache.tools.ant.BuildException;
Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;
Import Org.apache.tools.ant.taskdefs.Zip;
Import Org.apache.tools.ant.types.FileSet;
Import Java.io.File;
public class Zipper {
Public final static String encoding = "GBK";
Compression
public static void Zip (String srcpathname, String zipfilepath)
Throws Buildexception, RuntimeException {
File File = new file (srcpathname);
if (!file.exists ())
throw new RuntimeException ("source file or directory"
+ Srcpathname + "does not exist.");
Project proj = new Project ();
Fileset fileset = new Fileset ();
Fileset.setproject (proj);
Determine whether a directory or a file
if (File.isdirectory ()) {
Fileset.setdir (file);
Include/exclude rules in Ant are available here
Like what:
Fileset.setexcludes ("**/*.txt");
Fileset.setincludes ("**/*.xls");
} else {
Fileset.setfile (file);
}
Zip zip = new zip ();
Zip.setproject (proj);
Zip.setdestfile (New File (Zipfilepath));
Zip.addfileset (Fileset);
zip.setencoding (encoding);
Zip.execute ();
}
Unzip
public static void Unzip (string Zipfilepath (//destination file), string DestDir (//target path)
Throws Buildexception, RuntimeException {
if (!new File (Zipfilepath). Exists ())
throw new RuntimeException ("Zip file" + Zipfilepath
+ "does not exist.");
Project proj = new Project ();
Expand Expand = new Expand ();
Expand.setproject (proj);
Expand.settasktype ("unzip");
Expand.settaskname ("unzip");
expand.setencoding (encoding);
EXPAND.SETSRC (New File (Zipfilepath));
Expand.setdest (New File (DestDir));
Expand.execute ();
}
Create a Directory
public boolean Createdir (String destdirname) {
File dir = new file (destdirname);
if (dir.exists ()) {//Determine if the directory exists
System.out.println ("Failed to create directory, destination directory already exists!") ");
Boolean result = DeleteDirectory (destdirname);
if (result) {
Dir.mkdirs ();
return true;
}
return false;
}
if (!destdirname.endswith (File.separator)) {//ends with "/" End
destDirName = destDirName + file.separator;
}
if (Dir.mkdirs ()) {//Create destination directory
SYSTEM.OUT.PRINTLN ("Create directory successfully! "+ destdirname);
return true;
} else {
System.out.println ("Failed to create directory! ");
return false;
}
}
public boolean deletedirectory (String dirpath) {//delete directory (folder) and file under directory
Boolean flag = false;
If Spath does not end with a file delimiter, the file delimiter is automatically added
if (!dirpath.endswith (File.separator)) {
Dirpath = Dirpath + file.separator;
}
File Dirfile = new file (Dirpath);
If the dir corresponding file does not exist, or is not a directory, exit
if (!dirfile.exists () | |!dirfile.isdirectory ()) {
return false;
}
Flag = true;
file[] files = dirfile.listfiles ();//Get all files under the incoming path
for (int i = 0; i < files.length; i++) {//Iterate through all files under Delete folder (including subdirectories)
if (Files[i].isfile ()) {//delete sub-file
Flag = DeleteFile (Files[i].getabsolutepath ());
System.out.println (Files[i].getabsolutepath () + "Delete succeeded");
if (!flag)
break;//If the deletion fails, jump out of the
} else {//use recursion to delete subdirectories
Flag = DeleteDirectory (Files[i].getabsolutepath ());
if (!flag)
break;//If the deletion fails, jump out of the
}
}
if (!flag)
return false;
if (Dirfile.delete ()) {//delete current directory
return true;
} else {
return false;
}
}
public boolean DeleteFile (String filePath) {//delete individual files
Boolean flag = false;
File file;
Flag = false;
File = new file (FilePath);
if (File.isfile () && file.exists ()) {//path is file and not empty then delete
File.delete ();//File deletion
Flag = true;
}
return flag;
}
/* public static void Main (string[] args) {
String dir = new String ("f:\\ my backup \ \ Document \\myeclipse+9.0 official version cracked and active (pro-Test available)");
dir = new String ("f:/111.jpg");
Zip (dir, "F:/bzbxb/zipant.zip");
Unzip ("F:/desktop.zip", "f:/test/");
}*/
}
Parse the file inside
Parsing Excel
Public list<mpmsfiles> parsempfiles (String sb) {
try {
List<string[]> List=null;
Csvreader reader = new Csvreader (new InputStreamReader (New FileInputStream (SB), "GBK"));
List= Reader.readall ();
list<mpmsfiles> list5 = new arraylist<mpmsfiles> ();
if (List!=null&&list.size () >0) {
for (int j=0;j<list.size (); j + +) {
Mpmsfiles e = new Mpmsfiles ();
int length =list.get (j). Length;
if (length==1) {
if (! Stringutils.isempty (List.get (j) [0])) {
return null;
}
else{
return null;//Empty file
}
}
else if (length==2) {
if (Stringutils.isempty (List.get (j) [0])) {
return null;
}
if (Stringutils.isempty (List.get (j) [1])) {
return null;
}
else{
return null;
}
}
else if (length==3) {
if (Stringutils.isempty (List.get (j) [0])) {
return null;
}
if (Stringutils.isempty (List.get (j) [1]) | | Stringutils.isempty (List.get (j) [2])) {
return null;
}
E.setquestion (List.get (j) [0]);
E.setanswer (List.get (j) [1]);
E.setkeyword (List.get (j) [2]);
}
else {
return null;
}
List5.add (e);
}
Reader.close ();
return LIST5;
}
else{
return null;
}
}catch (Exception e) {
E.printstacktrace ();
}
}
Java Gadget--zip Package decompression, delete directories and files,