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