Use Ant programming to package simple files

Source: Internet
Author: User

Use Ant programming to package simple files

Kongxx

Ant is a familiar Java build tool. Generally we use Ant to build. the xml file is used to call a task. In fact, we can also use programming to call a task to simplify our development. The following is a program fragment I wrote a long time ago about using Ant to package files. Recently I flipped it out and simply sorted it out. The program fragment is as follows:

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 ();

}

}

From the code perspective, just a few lines of code solves the problem that we need to write dozens of lines of code. In fact, Ant can do a lot of work for us, and it can be implemented only through a few lines of code, why not.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.