java文件处理之压缩,分割
http://blog.csdn.net/ycg01/article/details/1366648
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
一.简单文件压缩,解压
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.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);
}
}
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();
}
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();
}