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. ...
随机推荐
- uilabel 复制
//添加一个长按响应方法 - (void)addLongPressGestureRecognizer { UILongPressGestureRecognizer * longPress = [[UI ...
- 汇总jQuery的61种选择器及示例
汇总jQuery的61种选择器及示例 恋痿喃 ㄍń稀广 因罘乐睽 ú燔蒇 骤幄觳 ч豹 齑骝氮铷 宅廓Ω孓 锏遒 荛猩ㄜ彬 芡钷ж ┊贩错鹌 掩饰着可还是几步就窜到了门口看着这个让她 ...
- iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html #import "ViewController.h" #import < ...
- CVE-2014-1767 漏洞分析(2015.1)
CVE-2014-1767 漏洞分析 1. 简介 该漏洞是由于Windows的afd.sys驱动在对系统内存的管理操作中,存在着悬垂指针的问题.在特定情况下攻击者可以通过该悬垂指针造成内存的doubl ...
- python 自动认证登录
import urllib import base64 import urllib2 def auto_login(urllink,username,password): authstr = 'Bas ...
- css3-多列显示文字
-------------------------- <style> #demo{ margin: 20px auto; ...
- mvc 母版页、用户自定义控件
母版页(Master) 1.母版页是与Controller无关的,母版页只是一个View文件,而没有任何Controller与之相对应. 2.其实在ASP.NET MVC中View的aspx与母版页并 ...
- Entity Framework教程
随着Code First一起出现的DbContext和DbSet类绝对可以称得上EF的功能核心,其取代了之前的ObjectContext和ObjectSet类,提供了与数据库通信,管理内存中实体的重要 ...
- Git 操作标签的一些命令
如果标签打错了,也是可以删除: $ git tag -d v0.1Deleted tag 'v0.1' (was d96a49b) 如果要推送某个标签到远程,使用git push orign tagn ...
- Qt之Windows开发移植问题汇总
来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101auw6.html 在用Qt开发完成项目后,就需要将其打包并且移植在其他机器上,能在其他PC机上正常跑起来才 ...