java文件处理之压缩,分割

一.简单文件压缩,解压

 

package  cn.edu.nju.vicken;

import  java.io.BufferedInputStream;
import  java.io.BufferedOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.InputStream;
import  java.io.OutputStream;
import  java.util.ArrayList;
import  java.util.Enumeration;
import  java.util.List;
import  java.util.zip.ZipEntry;
import  java.util.zip.ZipFile;
import  java.util.zip.ZipOutputStream;

/**
 * 
 * 
@author VickenYang
 * 文件处理工具
 
*/

public   class  FileProcessor  {
    
    
public static void createZipFile(File from,File to) throws Exception {
        
if(!from.isFile()){
            
throw new Exception("file not exists"+from.getAbsolutePath());
        }

        
if(to.isFile()){
            
throw new Exception("file already exists"+to.getAbsolutePath());
        }

        
else{
            to.createNewFile();
        }

        
        ZipOutputStream zos 
= new ZipOutputStream(new FileOutputStream(to));
        ZipEntry ze 
= null;
        
byte[] buf = new byte[1024];
        
int readLen = 0;
        ze 
= new ZipEntry(from.getAbsolutePath());
        ze.setSize(from.length());
        ze.setTime(from.lastModified());
        zos.putNextEntry(ze);
        InputStream is 
= new BufferedInputStream(new FileInputStream(from));
        
while ((readLen=is.read(buf, 01024))!=-1{
            zos.write(buf, 
0, readLen);
        }

        is.close();
        zos.close();
    }

    
    
public static void decompressZipFile(File from,File to) throws Exception{
        
if(!from.isFile()){
            
throw new Exception("file not exists"+from.getAbsolutePath());
        }

        
if(!to.isDirectory()){
            
throw new Exception("file not directory"+to.getAbsolutePath());
        }

        ZipFile zfile 
= new ZipFile(from.getAbsoluteFile());
        Enumeration zList 
= zfile.entries();
        ZipEntry ze
=null;
        
byte[] buf=new byte[1024];
        
while(zList.hasMoreElements()){
            ze
=(ZipEntry)zList.nextElement();
            
if(ze.isDirectory()){
                
continue;
            }

            String[] zet 
= ze.getName().replace('/''/').split("/");
            OutputStream os
=new BufferedOutputStream(new FileOutputStream(to.getAbsoluteFile()+zet[zet.length-1]));
            InputStream is
=new BufferedInputStream(zfile.getInputStream(ze));
            
int readLen=0;
            
while ((readLen=is.read(buf, 01024))!=-1{
                os.write(buf, 
0, readLen);
            }

            is.close();
            os.close();
        }

        zfile.close();
    }

}

 二.定时器程序

 

import  java.util.Timer;
import  java.util.TimerTask;
class  MyTask  extends  TimerTask {
    String name;
    
public MyTask(String name){
        
this.name = name;
    }

    
public void run(){
        System.out.println(name);
    }

}

public   class  TestTask  {
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        TimerTask task = new MyTask("10秒执行一次");
        java.util.Date today 
= new java.util.Date();
        
//开始时间设置为昨天
        java.util.Date beginTime = new java.util.Date(today.getYear(),today.getMonth(),today.getDate()-1,22,0,0);
        
//10秒一次
        new Timer().schedule(task,beginTime,1000*10);
        
//执行一次
        task = new MyTask("只执行一次");
        
new Timer().schedule(task,beginTime);
    }

}

 

三.分割,合并文件

 

// 拆分文件
     public   static   void  splitFile(File file, int  size)  throws  Exception {
        
if(size<=0){
            size 
= 1024;
        }

        
if(!file.isFile()){
            
throw new Exception("file not exists"+file.getAbsolutePath());
        }

        String filename 
= file.getAbsolutePath();
        File filetmp 
= new File(filename+"_"+0+".vk");
        
if(filetmp.isFile()){
            
throw new Exception("file exists"+filetmp.getAbsolutePath());
        }

        
        
byte[] buf = new byte[1024*10];
        FileInputStream fis 
= new FileInputStream(file);
        
int readsize = 0;
        
int pos = 0;
        
int k = 0;
        
int m = -1;
        File fileout 
= null;
        FileOutputStream fos 
= null;
        
while((readsize = fis.read(buf, 0, buf.length))>0){
            
            
if(k!=m)
            
{
                
if(fos!=null){
                    fos.close();
                    fos 
= null;
                }

                m 
= k;
                fileout 
= new File(filename+"_"+k+".vk");
                fos 
= new FileOutputStream(fileout);
            }

            fos.write(buf,
0,readsize);
            fos.flush();
            pos 
+= readsize;
            
if(pos>size*(k+1)){
                k
++;
            }

        }

        
if(fos!=null){
            fos.close();
            fos 
= null;
        }

        fis.close();
    }

    
    
    
// 合并文件
     public   static   void  combination(File file)  throws  Exception {
        String filename 
= file.getAbsolutePath();
        File fileout 
= new File(filename);
        
        
if(fileout.isFile()){
            
throw new Exception("file exists"+fileout.getAbsolutePath());
        }

        FileOutputStream fos 
= new FileOutputStream(fileout);
        
int k = 0;
        File filein 
= null;
        FileInputStream fis 
= null;
        
byte[] buf = new byte[1024*10];
        
while(true){
            
if(fis!=null){
                fis.close();
                fis 
= null;
            }

            filein 
= new File(filename+"_"+k+".vk");
            
if(!filein.isFile()){
                
break;
            }

            fis 
= new FileInputStream(filein);
            
int readsize = 0;
            
while((readsize = fis.read(buf, 0, buf.length))>0){
                fos.write(buf,
0,readsize);
                fos.flush();
            }

            k
++;
        }

        
if(fis!=null){
            fis.close();
            fis 
= null;
        }

        fos.close();
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值