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.