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

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值