Java split file and merge files (Zip,rar ....)

Source: Internet
Author: User
Tags rar

A Java split file that splits a zip file by a specified size and then merges it together.

Segmentation

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 splitting files");
Split (SrcName, size, destname);
SYSTEM.OUT.PRINTLN ("File partition complete");
}
private static void Split (String src,int mb,string dest) {
TODO auto-generated method stubs
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) {
TODO Auto-generated catch block
E.printstacktrace ();
} catch (IOException e) {
TODO Auto-generated catch block
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 ("merger success");
}
private static void Merge (String dest,file. files) {
TODO auto-generated method stubs
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<files.length;i++)
{
Bis=new Bufferedinputstream (New FileInputStream (Files[i]));
while ((Len=bis.read (bytes))!=-1) {
Bos.write (bytes, 0, Len);
}
}
} catch (FileNotFoundException e) {
TODO Auto-generated catch block
E.printstacktrace ();
} catch (IOException e) {
TODO Auto-generated catch block
E.printstacktrace ();
}

}
}

Java split file and merge files (Zip,rar ....)

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.