Import java. io. File; 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; Public class Package { Public static void main (String [] args ){ Package p1 = new Package (); String strDir1 = "f: // Test "; String strDestFile1 = "f: // test1.zip "; P1.pack (strDir1, strDestFile1 ); Package p2 = new Package (); String strDir2 = "f: // Test "; String strDestFile2 = "f: // test2.zip "; String [] filenames2 = {"test1 // test11 // *", "test1 // test12 //*"}; P2.pack (strDir2, filenames2, strDestFile2 ); } /** * Package a directory * @ Param strDir the directory to be packaged * @ Param strDestFile: Path of the compressed file to be packaged */ Public void pack (String strDir, String strDestFile ){ Pack (new File (strDir), new File (strDestFile )); } /** * Package a directory * @ Param fDir the directory object to be packaged * @ Param fDestFile: the compressed file object to be packaged */ Public void pack (File fDir, File fDestFile ){ Zip zip = new Zip (); Zip. setProject (new Project ()); Zip. setTaskName ("default "); Zip. setBasedir (fDir ); Zip. setDestFile (fDestFile ); Zip.exe cute (); } /** * Package a file within a certain directory * @ Param fDir the directory object to be packaged * @ Param filenames: list of objects to be included. Is the relative path of the directory to be packaged * @ Param fDestFile: the compressed file object to be packaged */ Public void pack (File fDir, String [] filenames, File fDestFile ){ Zip zip = new Zip (); Zip. setProject (new Project ()); Zip. setTaskName ("default "); Zip. setDestFile (fDestFile ); FileSet fs = new FileSet (); Fs. setDir (fDir ); For (int I = 0; I <filenames. length; I ++ ){ Fs. setIncludes (filenames [I]); } Zip. addFileset (fs ); Zip.exe cute (); } /** * Package a file within a certain directory * @ Param strDir the directory object to be packaged * @ Param filenames: list of objects to be included. Is the relative path of the directory to be packaged * @ Param strDestDir the compressed file object to be packaged */ Public void pack (String strDir, String [] filenames, String strDestDir ){ Pack (new File (strDir), filenames, new File (strDestDir )); } /** * * @ Param strSrcZip * @ Param strDestDir */ Public void unpack (String strSrcZip, String strDestDir ){ Unpack (new File (strSrcZip), new File (strDestDir )); } /** * * @ Param fSrcZip * @ Param fDestDir */ Public void unpack (File fSrcZip, File fDestDir ){ Expand expand = new Expand (); Expand. setProject (new Project ()); Expand. setSrc (fSrcZip ); Expand. setDest (fDestDir ); Expand. setOverwrite (true ); Expand.exe cute (); } } |