天天看點

java分割檔案并且合并檔案(zip,rar....)

JAVA分割檔案,把一個ZIP檔案通過指定的大小分割,然後合并起來。

分割

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("開始分割檔案");

  split(srcName, size, destName);

  System.out.println("檔案分割完成");

 }

 private static void split(String src,int mb,String dest) {

  // 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) {

   // TODO 自動生成的 catch 塊

   e.printStackTrace();

  } catch (IOException e) {

   // TODO 自動生成的 catch 塊

   e.printStackTrace();

  }

 }

}

合并

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("開始合并");

  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("合并成功");

 }

 private static void merge(String dest,File... files) {

  // 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<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 自動生成的 catch 塊

   e.printStackTrace();

  } catch (IOException e) {

   // TODO 自動生成的 catch 塊

   e.printStackTrace();

  }

 }

}