java文件处理之压缩,分割
生活随笔
收集整理的這篇文章主要介紹了
java文件处理之压缩,分割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://blog.csdn.net/ycg01/article/details/1366648
java文件處理之壓縮,分割
標簽:?javaexceptionimportnullbytefile 2006-11-05 00:30?1574人閱讀?評論(1)?收藏?舉報 ?分類: ? 點滴(12)?版權聲明:本文為博主原創文章,未經博主允許不得轉載。
一.簡單文件壓縮,解壓
?
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,?0,?1024))!=-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,?0,?1024))!=-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();
????}
轉載于:https://www.cnblogs.com/donaldlee2008/p/5312118.html
總結
以上是生活随笔為你收集整理的java文件处理之压缩,分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [密码学基础][每个信息安全博士生应该知
- 下一篇: Docker image Introdu