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.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();
}java文件处理之压缩,分割的更多相关文章
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
- Java I/O 文件加锁,压缩
文件加锁: 文件加锁机制允许我们同步访问某个作为共享资源的文件. public class Test { public static void main(String[] args) throws I ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- java zip4j 内存文件和磁盘文件 压缩和加密
经常服务器需要对文件进行压缩,网络上流传较多的是从磁盘文件中来压缩成zip文件.但是常常服务器的文件存放在内存中,以byte[]形式存储在内存中.这个时候就不能使用网络上流传的常用方法了,这里就需要对 ...
- Java解压和压缩带密码的zip或rar文件(下载压缩文件中的选中文件、向压缩文件中新增、删除文件)
JAVA 实现在线浏览管理zip和rar的工具类 (有密码及无密码的)以及下载压缩文件中的选中文件(向压缩文件中新增.删除文件) 这是之前的版本 JAVA 解压压缩包中指定文件或实现压缩文件的预览及下 ...
- java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件
/** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...
- Java文件操作源码大全
Java文件操作源码大全 1.创建文件夹 52.创建文件 53.删除文件 54.删除文件夹 65.删除一个文件下夹所有的文件夹 76.清空文件夹 87.读取文件 88.写入文件 99.写入随机文件 9 ...
- Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...
- 把java文件打包成.jar (jar命令详解)
把java文件打包成.jar (jar命令详解) 先打开命令提示符(win2000或在运行框里执行cmd命令,win98为DOS提示符),输入jar Chelp,然后回车(如果你盘上已经有了jdk1. ...
随机推荐
- 锅巴H264播放器地址和说明
锅巴H264播放器地址和说明 软件说明: 此工具专门用来播放安防监控行业的H264录像文件,不管是哪个设备厂家的视频协议,只要您的录像文件里有 H264数据,就可以播放. 备注: 因为被一些事情的影 ...
- SQL Server 2012中快速插入批量数据的示例及疑惑
SQL Server 2008中SQL应用系列--目录索引 今天在做一个案例演示时,在SQL Server 2012中使用Insert语句插入1万条数据,结果遇到了一个奇怪的现象,现将过程分享出来,以 ...
- js中call方法的使用介绍
js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, ...
- 《Windows驱动开发技术详解》之IRP的同步
应用程序对设备的同步异步操作: 大部分IRP都是由应用程序的Win32 API函数发起的.这些Win32 API本身就支持同步和异步操作.例如,ReadFile.WriteFile和DeviceIoC ...
- Chapter 1 First Sight——30
The girl sitting there giggled. I'd noticed that his eyes were black — coal black. 那个坐在那里的女孩笑着.我注意到她 ...
- Linux系统手动安装rpm包依赖关系分析(以Kernel升级为例)
有在Linux系统中安装软件的经历的人都知道,在Linux系统中手动安装软件不想在Windows下安装软件那么方便,直接双击,然后下一步下一步就可以把软件成功的装入到系统中,而在Linux系统中,安装 ...
- debian下安装repo
1.去google网站上下载repo脚本(用php语言写成的脚本) https://gerrit.googlesource.com/git-repo/+/stable/repo 可以将脚本复制下来并保 ...
- hdu 1425 sort
Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行 ...
- how to use the curses library in unix?
In linux, you can use the ncurses library to use the terminal as a text buffer: move the cursor arou ...
- ubuntu 连接VPN 命令
1 注册MXVPN 2 启动VPN pptpsetup --create mxvpn1 --server xx.xx.xx.xx --username *** --password *** --e ...