java文件处理之压缩,分割
http://blog.csdn.net/ycg01/article/details/1366648
java文件处理之压缩,分割
版权声明:本文为博主原创文章,未经博主允许不得转载。
一.简单文件压缩,解压
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.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文件处理之压缩,分割相关推荐
- java 文件压缩 解压_Java文件压缩与解压缩(一)
package com.cn; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...
- java 解压与压缩代码_Java实现多文件压缩和解压缩代码详解
Java实现多文件压缩和解压缩代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...
- 大文件做分割处理的方法——winRAR压缩分割法
###可以把一些大的文件压缩成多个小文件,享受压缩.分割文件 1,开始分割: 2,分割后:(压缩.分割文件) 完成设置后,单击"确定"按钮,这样WinRAR压缩软件就会把软件压缩并 ...
- 如何将文件打包、压缩并分割成制定大小?
如何将文件打包.压缩并分割成制定大小? (如何在linux下实现Windows中winrar和winzip等压缩软件所提供的打包压缩分割功能) 将大文件或目录打包.压缩并分割成制定大小的文件,这在将数 ...
- java文件和xml文件_用Java分割大型XML文件
java文件和xml文件 上周,我被要求用Java编写一些东西,该东西能够将一个30GB的XML文件拆分为可配置文件大小的较小部分. 文件的使用者将是一个中间件应用程序,该应用程序在XML的大尺寸方面 ...
- java.util.zip_[Java 基础] 使用java.util.zip包压缩和解压缩文件
Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作. Zi ...
- java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
全栈工程师开发手册 (作者:栾鹏) java教程全解 java使用tar算法压缩解压缩文件.数据流.byte[]字节数组 需要添加org.apache.commons.compress包,下载 测试代 ...
- java使用BZip算法压缩解压缩文件、数据流、byte[]字节数组
全栈工程师开发手册 (作者:栾鹏) java教程全解 java使用BZip算法压缩解压缩文件.数据流.byte[]字节数组 需要添加org.apache.commons.compress包,下载 测试 ...
- java 文件分割器_Java分割器
java 文件分割器 Java Spliterator is one of the four iterators – Enumeration, Iterator, ListIterator and S ...
最新文章
- SQL Cursor(游标)
- 社交系统/社群系统ThinkSNS+ alpha.2 版本发布!
- [Microsoft][ODBC SQL Server Driver][SQl Server]参数数据类型 text 对于 replace 函数的参数 1 无效。...
- python的全局变量能暂存数据吗_Python 中的全局变量 局部变量
- UVA - 11214Guarding the Chessboard守卫棋盘(迭代加深搜索)
- linux perl模块检测,Linux有问必答:如何用Perl检测Linux的发行版本
- python中的深浅拷贝
- or函数 java_Java OptionalInt orElseGet()用法及代码示例
- memcached命令行、Memcached数据导出和导入、php连接memcache、php的session存储到memcached...
- matlab 箱图不显示异常值_欧姆龙E5EZ温控器PV显示值跳动异常的处理,一看就懂...
- 西南科技大学OJ题 最简单的C程序0612
- mysql relay log 修改_MySQL relaylog + SQL_Thread 增量恢复binlog
- SQL(08)_SQL约束
- 在Spring Boot中spring mvc常见注解解析及部分源码
- oracle sql 自动补位数
- 号码认证一键免密登录,让验证更简单!
- CAD转换jpg的使用大全
- 离线电影管理软件 极影派
- linux设置时间自动同步
- 中本聪论文_浏览器中本机智能卡支持的案例
热门文章
- CNN反向传播卷积核翻转
- Python 中的Pandas库
- python——获取数据类型
- poj3713 Transferring Sylla 枚举+tarjan判割点
- mysql_表_操作
- jekins构建触发器详解
- 使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling
- Hadoop 使用FileSystem API 读取数据
- 转-HTC 手机生产日期,产地查询 以及 SN码 IMEI码 查询等问题汇总
- 远程链接错误:这可能是由于credssp加密oracle修正