日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java文件处理之压缩,分割

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件处理之压缩,分割 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/ycg01/article/details/1366648

java文件處理之壓縮,分割

標簽:?javaexceptionimportnullbytefile ?分類: ? 點滴(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文件处理之压缩,分割的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。