http://blog.csdn.net/ycg01/article/details/1366648
java文件处理之压缩,分割
标签: javaexceptionimportnullbytefile
 2006-11-05 00:30 1574人阅读 评论(1) 收藏 举报
  分类:
 分类:点滴(12) 
 
版权声明:本文为博主原创文章,未经博主允许不得转载。
一.简单文件压缩,解压
 package cn.edu.nju.vicken;
package cn.edu.nju.vicken;
 import java.io.BufferedInputStream;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream;
import java.io.BufferedOutputStream; import java.io.File;
import java.io.File; import java.io.FileInputStream;
import java.io.FileInputStream; import java.io.FileOutputStream;
import java.io.FileOutputStream; import java.io.InputStream;
import java.io.InputStream; import java.io.OutputStream;
import java.io.OutputStream; import java.util.ArrayList;
import java.util.ArrayList; import java.util.Enumeration;
import java.util.Enumeration; import java.util.List;
import java.util.List; import java.util.zip.ZipEntry;
import java.util.zip.ZipEntry; import java.util.zip.ZipFile;
import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream;
import java.util.zip.ZipOutputStream;
 /**
/** *
 *  * @author VickenYang
 * @author VickenYang * 文件处理工具
 * 文件处理工具 */
 */ public class FileProcessor {
public class FileProcessor { 
     public static void createZipFile(File from,File to) throws Exception {
    public static void createZipFile(File from,File to) throws Exception { if(!from.isFile()){
        if(!from.isFile()){ throw new Exception("file not exists"+from.getAbsolutePath());
            throw new Exception("file not exists"+from.getAbsolutePath()); }
        } if(to.isFile()){
        if(to.isFile()){ throw new Exception("file already exists"+to.getAbsolutePath());
            throw new Exception("file already exists"+to.getAbsolutePath()); }
        } else{
        else{ to.createNewFile();
            to.createNewFile(); }
        } 
         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(to));
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(to)); ZipEntry ze = null;
        ZipEntry ze = null; byte[] buf = new byte[1024];
        byte[] buf = new byte[1024]; int readLen = 0;
        int readLen = 0; ze = new ZipEntry(from.getAbsolutePath());
        ze = new ZipEntry(from.getAbsolutePath()); ze.setSize(from.length());
        ze.setSize(from.length()); ze.setTime(from.lastModified());
        ze.setTime(from.lastModified()); zos.putNextEntry(ze);
        zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(from));
        InputStream is = new BufferedInputStream(new FileInputStream(from)); while ((readLen=is.read(buf, 0, 1024))!=-1) {
        while ((readLen=is.read(buf, 0, 1024))!=-1) { zos.write(buf, 0, readLen);
            zos.write(buf, 0, readLen); }
        } is.close();
        is.close(); zos.close();
        zos.close(); }
    } 
     public static void decompressZipFile(File from,File to) throws Exception{
    public static void decompressZipFile(File from,File to) throws Exception{ if(!from.isFile()){
        if(!from.isFile()){ throw new Exception("file not exists"+from.getAbsolutePath());
            throw new Exception("file not exists"+from.getAbsolutePath()); }
        } if(!to.isDirectory()){
        if(!to.isDirectory()){ throw new Exception("file not directory"+to.getAbsolutePath());
            throw new Exception("file not directory"+to.getAbsolutePath()); }
        } ZipFile zfile = new ZipFile(from.getAbsoluteFile());
        ZipFile zfile = new ZipFile(from.getAbsoluteFile()); Enumeration zList = zfile.entries();
        Enumeration zList = zfile.entries(); ZipEntry ze=null;
        ZipEntry ze=null; byte[] buf=new byte[1024];
        byte[] buf=new byte[1024]; while(zList.hasMoreElements()){
        while(zList.hasMoreElements()){ ze=(ZipEntry)zList.nextElement();
            ze=(ZipEntry)zList.nextElement(); if(ze.isDirectory()){
            if(ze.isDirectory()){ continue;
                continue; }
            } String[] zet = ze.getName().replace('/', '/').split("/");
            String[] zet = ze.getName().replace('/', '/').split("/"); OutputStream os=new BufferedOutputStream(new FileOutputStream(to.getAbsoluteFile()+zet[zet.length-1]));
            OutputStream os=new BufferedOutputStream(new FileOutputStream(to.getAbsoluteFile()+zet[zet.length-1])); InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
            InputStream is=new BufferedInputStream(zfile.getInputStream(ze)); int readLen=0;
            int readLen=0; while ((readLen=is.read(buf, 0, 1024))!=-1) {
            while ((readLen=is.read(buf, 0, 1024))!=-1) { os.write(buf, 0, readLen);
                os.write(buf, 0, readLen); }
            } is.close();
            is.close(); os.close();
            os.close(); }
        } zfile.close();
        zfile.close(); }
    } }
}
二.定时器程序
 import java.util.Timer;
import java.util.Timer; import java.util.TimerTask;
import java.util.TimerTask; class MyTask extends TimerTask{
class MyTask extends TimerTask{ String name;
    String name; public MyTask(String name){
    public MyTask(String name){ this.name = name;
        this.name = name; }
    } public void run(){
    public void run(){ System.out.println(name);
        System.out.println(name); }
    } }
} public class TestTask {
public class TestTask { public static void main(String[] args) {
    public static void main(String[] args) { // TODO Auto-generated method stub
        // TODO Auto-generated method stub TimerTask task = new MyTask("10秒执行一次");
        TimerTask task = new MyTask("10秒执行一次"); java.util.Date today = new java.util.Date();
        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);
        java.util.Date beginTime = new java.util.Date(today.getYear(),today.getMonth(),today.getDate()-1,22,0,0); //10秒一次
        //10秒一次 new Timer().schedule(task,beginTime,1000*10);
        new Timer().schedule(task,beginTime,1000*10); //执行一次
        //执行一次 task = new MyTask("只执行一次");
        task = new MyTask("只执行一次"); new Timer().schedule(task,beginTime);
        new Timer().schedule(task,beginTime); }
    } }
}
三.分割,合并文件
 //拆分文件
//拆分文件 public static void splitFile(File file,int size) throws Exception{
    public static void splitFile(File file,int size) throws Exception{ if(size<=0){
        if(size<=0){ size = 1024;
            size = 1024; }
        } if(!file.isFile()){
        if(!file.isFile()){ throw new Exception("file not exists"+file.getAbsolutePath());
            throw new Exception("file not exists"+file.getAbsolutePath()); }
        } String filename = file.getAbsolutePath();
        String filename = file.getAbsolutePath(); File filetmp = new File(filename+"_"+0+".vk");
        File filetmp = new File(filename+"_"+0+".vk"); if(filetmp.isFile()){
        if(filetmp.isFile()){ throw new Exception("file exists"+filetmp.getAbsolutePath());
            throw new Exception("file exists"+filetmp.getAbsolutePath()); }
        } 
         byte[] buf = new byte[1024*10];
        byte[] buf = new byte[1024*10]; FileInputStream fis = new FileInputStream(file);
        FileInputStream fis = new FileInputStream(file); int readsize = 0;
        int readsize = 0; int pos = 0;
        int pos = 0; int k = 0;
        int k = 0; int m = -1;
        int m = -1; File fileout = null;
        File fileout = null; FileOutputStream fos = null;
        FileOutputStream fos = null; while((readsize = fis.read(buf, 0, buf.length))>0){
        while((readsize = fis.read(buf, 0, buf.length))>0){ 
             if(k!=m)
            if(k!=m) {
            { if(fos!=null){
                if(fos!=null){ fos.close();
                    fos.close(); fos = null;
                    fos = null; }
                } m = k;
                m = k; fileout = new File(filename+"_"+k+".vk");
                fileout = new File(filename+"_"+k+".vk"); fos = new FileOutputStream(fileout);
                fos = new FileOutputStream(fileout); }
            } fos.write(buf,0,readsize);
            fos.write(buf,0,readsize); fos.flush();
            fos.flush(); pos += readsize;
            pos += readsize; if(pos>size*(k+1)){
            if(pos>size*(k+1)){ k++;
                k++; }
            } }
        } if(fos!=null){
        if(fos!=null){ fos.close();
            fos.close(); fos = null;
            fos = null; }
        } fis.close();
        fis.close(); }
    } 
     
     //合并文件
    //合并文件 public static void combination(File file) throws Exception{
    public static void combination(File file) throws Exception{ String filename = file.getAbsolutePath();
        String filename = file.getAbsolutePath(); File fileout = new File(filename);
        File fileout = new File(filename); 
         if(fileout.isFile()){
        if(fileout.isFile()){ throw new Exception("file exists"+fileout.getAbsolutePath());
            throw new Exception("file exists"+fileout.getAbsolutePath()); }
        } FileOutputStream fos = new FileOutputStream(fileout);
        FileOutputStream fos = new FileOutputStream(fileout); int k = 0;
        int k = 0; File filein = null;
        File filein = null; FileInputStream fis = null;
        FileInputStream fis = null; byte[] buf = new byte[1024*10];
        byte[] buf = new byte[1024*10]; while(true){
        while(true){ if(fis!=null){
            if(fis!=null){ fis.close();
                fis.close(); fis = null;
                fis = null; }
            } filein = new File(filename+"_"+k+".vk");
            filein = new File(filename+"_"+k+".vk"); if(!filein.isFile()){
            if(!filein.isFile()){ break;
                break; }
            } fis = new FileInputStream(filein);
            fis = new FileInputStream(filein); int readsize = 0;
            int readsize = 0; while((readsize = fis.read(buf, 0, buf.length))>0){
            while((readsize = fis.read(buf, 0, buf.length))>0){ fos.write(buf,0,readsize);
                fos.write(buf,0,readsize); fos.flush();
                fos.flush(); }
            } k++;
            k++; }
        } if(fis!=null){
        if(fis!=null){ fis.close();
            fis.close(); fis = null;
            fis = null; }
        } fos.close();
        fos.close(); }
    }