Compress folders into jar packages-JAVA gadgets

Source: Internet
Author: User
Download Tool

We often encounter changes to the content in the Jar package. The previous method is to decompress the package, compress it to zip, and change the suffix to jar.

This java tool can be used to compress files in batches into jar packages. The core code is as follows:

/**
* Zip Compression
* @ Param parentDirPath: parent folder of the folder to be compressed
* @ Param targetPath: Target folder
*/
Private static void zipDirectory (String parentDirPath, String targetPath)
{
Try {
File dirFile = new File (parentDirPath );
File [] listArr = dirFile. listFiles ();
For (File childFile: listArr ){
// File childFile = new File (child );
If (childFile. isDirectory ())
{
If (list. size ()> 0)
List. clear ();
Byte B [] = new byte [128];
// Save path of the compressed file
String zipFile = targetPath + File. separator + childFile. getName () + ". jar ";

// Compressed file directory
String filepath = childFile. getAbsolutePath () + File. separator;

List fileList = allFile (filepath );

FileOutputStream fileOutputStream = new FileOutputStream (zipFile );
// Use output stream check
CheckedOutputStream cs = new CheckedOutputStream (fileOutputStream, new CRC32 ());
// Declare the output zip stream
ZipOutputStream out = new ZipOutputStream (new BufferedOutputStream (
Cs ));

For (int I = 0; I <fileList. size (); I ++ ){
InputStream in = new FileInputStream (String) fileList. get (I ));
String fileName = (String) (fileList. get (I). replace (File. separatorChar ,'/');
System. out. println ("ziping" + fileName );
String tmp = childFile. getName () + "/";
FileName = fileName. substring (fileName. lastIndexOf (tmp) + childFile. getName (). length () + 1 );
ZipEntry e = new ZipEntry (fileName );
Out. putNextEntry (e );
Int len = 0;
While (len = in. read (B ))! =-1 ){
Out. write (B, 0, len );
}
Out. closeEntry ();
}
Out. close ();
System. out. println ("done! ");
}
}

} Catch (Exception e ){
E. printStackTrace ();
}

}

 

Private static List allFile (String path)
{

File file = new File (path );
String [] array = null;
String sTemp = "";

If (file. isDirectory ())
{
} Else {
Return null;
}
Array = file. list ();
If (array. length> 0)
{
For (int I = 0; I <array. length; I ++)
{
STemp = path + array [I];
File = new File (sTemp );
If (file. isDirectory ())
{
AllFile (sTemp + "/");

} Else {
List. add (sTemp );
}
}
} Else {
Return null;
}

Return list;
}

 

Tool usage: Download

The first parameter is the upper-level path of the folder to be compressed; the second parameter is the destination folder path.

 

Java source code project: source code download

 

This blog statement: I have received support from leaders of sinosam during my technical exploration. In the future, all technical exploration achievements in my blog will be jointly owned by "no trace", "guoxin Si Nan", and "blog Park". Original works should be reprinted if necessary, please note this blog statement.

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.