Java splits and merges files (zip, rar ....)

Source: Internet
Author: User

Java splits and merges files (zip, rar ....)

JAVA splits the file, splits the ZIP file by the specified size, and then merges it.

Split

Package com. zkq. objectstream;

Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;

Public class HomeWork1 {
Public static void main (String [] args ){
String srcName = "D: \ apache-tomcat-7.0.40-windows-x86.zip ";
String destName = "E: \ temp \\";
Int size = 1;
System. out. println ("start to split Files ");
Split (srcName, size, destName );
System. out. println ("file segmentation completed ");
}
Private static void split (String src, int mb, String dest ){
// The method stub automatically generated by TODO
File srcFile = new File (src );
If (! SrcFile. exists ())
{
Return;
}
Long countSize = srcFile. length ();
Long fileSize = 1024*1024 * mb;
Int num = 0;
If (countSize % fileSize = 0)
{
Num = (int) (countSize/fileSize );
}
Else
{
Num = (int) (countSize/fileSize) + 1;
}
InputStream in = null;
Try {
In = new FileInputStream (srcFile );
BufferedInputStream bis = new BufferedInputStream (in );
BufferedOutputStream bos = null;
Byte bytes [] = new byte [1024*1024];
Int len =-1;
For (int I = 0; I <num; I ++ ){
String newFile = dest + File. separator + srcFile. getName () + "-" + I;
Bos = new BufferedOutputStream (new FileOutputStream (newFile ));
Int count = 0;
While (len = bis. read (bytes ))! =-1 ){
Bos. write (bytes, 0, len );
Bos. flush ();
Count + = len;
If (count> = fileSize)
{
Break;
}

}
Bos. close ();
}
Bis. close ();
In. close ();
} Catch (FileNotFoundException e ){
// Catch Block automatically generated by TODO
E. printStackTrace ();
} Catch (IOException e ){
// Catch Block automatically generated by TODO
E. printStackTrace ();
}

}
}

Merge

Package com. zkq. objectstream;

Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;

Public class HomeWork2 {
Public static void main (String [] args ){
System. out. println ("start merging ");
Merge ("E: \ temp", new File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-0"), new File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-1 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-2 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-3 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-4 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-5 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-6 "),
New File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-7"), new File ("E: \ temp \ apache-tomcat-7.0.40-windows-x86.zip-8 "));
System. out. println ("merged successfully ");
}
Private static void merge (String dest, File... files ){
// The method stub automatically generated by TODO
String filename = files [0]. getName ();
Filename = files [0]. getName (). substring (0, filename. lastIndexOf ("-"));
Try {
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (dest + File. separator + filename ));
BufferedInputStream bis = null;
Byte bytes [] = new byte [1024*1024];
Int len =-1;
For (int I = 0; I {
Bis = new BufferedInputStream (new FileInputStream (files [I]);
While (len = bis. read (bytes ))! =-1 ){
Bos. write (bytes, 0, len );
}
}
} Catch (FileNotFoundException e ){
// Catch Block automatically generated by TODO
E. printStackTrace ();
} Catch (IOException e ){
// Catch Block automatically generated by TODO
E. printStackTrace ();
}

}
}

Related Article

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.