file压缩
来源:互联网 发布:详情页设计软件 编辑:程序博客网 时间:2024/05/22 22:32
package com.ziping;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class UnZip {
public static void main(String[] args){
zip(new File("e:/crmo.sql"),new File("c:/"+System.currentTimeMillis()+".zip"));
}
public static void unZipFile(File zipPath,File unZipPath)
{
ZipFile zipFile=null;
InputStream input = null;
OutputStream output = null;
try {
zipFile=new ZipFile(zipPath);
Enumeration<?> entries = zipFile.entries();
while(entries.hasMoreElements())
{
ZipEntry zipEntry = (ZipEntry)entries.nextElement();
if(zipEntry.isDirectory())
{
String nameString = zipEntry.getName();
System.out.println(nameString);
nameString = nameString.substring(0,nameString.length()-1);
unZipPath.mkdirs();
}
else
{
String filePath = unZipPath.getAbsolutePath()+File.separator+zipEntry.getName();
System.out.println(zipEntry.getName());
filePath = new String(filePath.getBytes(),"gbk");
File file = new File(filePath);
File parentFile= file.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
input = zipFile.getInputStream(zipEntry);
output = new FileOutputStream(file);
byte[] buffer = new byte[8092];
int realLength = 0;
while((realLength = input.read(buffer)) != -1)
{
output.write(buffer,0,realLength);
}
}
}
zipFile.close();
output.close();
input.close();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void zip(File sourceFolder, File zipFile) {
ZipOutputStream zos = null;
try {
ZipOutputStream zos2 = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
zipFile(sourceFolder, sourceFolder.isDirectory() ? sourceFolder.getPath() : sourceFolder.getParent(), zos2);
zos2.closeEntry();
zos2.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
public static void zip(String sourceDir, String zipFile) {
zip(new File(sourceDir), new File(zipFile));
}
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
// 创建文件对象用于装载要压缩文件
File[] files = new File[0];
String pathName;
int length = 0;
// 检测是否有子目录
if (source.isDirectory()) {
// 获取目录下所有文档
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
byte[] buf = new byte[1024];
try {
for (File file : files) {
if (file.isDirectory()) {
// 获得文件路径+“/”表示还有子目录
pathName = file.getPath().substring(basePath.length())
+ "/";
// 创建一个Zip文件目录进行传递给输出流
zos.putNextEntry(new ZipEntry(pathName));
// 递归压缩到没有子目录
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length());
// 打开文件输入通道
InputStream is = new FileInputStream(file);
// 使用缓冲流对接压缩流
BufferedInputStream bis = new BufferedInputStream(is);
// 创建压缩目录
zos.putNextEntry(new ZipEntry(pathName));
// 循环写入压缩流
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
// 关闭文件流
is.close();
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class UnZip {
public static void main(String[] args){
zip(new File("e:/crmo.sql"),new File("c:/"+System.currentTimeMillis()+".zip"));
}
public static void unZipFile(File zipPath,File unZipPath)
{
ZipFile zipFile=null;
InputStream input = null;
OutputStream output = null;
try {
zipFile=new ZipFile(zipPath);
Enumeration<?> entries = zipFile.entries();
while(entries.hasMoreElements())
{
ZipEntry zipEntry = (ZipEntry)entries.nextElement();
if(zipEntry.isDirectory())
{
String nameString = zipEntry.getName();
System.out.println(nameString);
nameString = nameString.substring(0,nameString.length()-1);
unZipPath.mkdirs();
}
else
{
String filePath = unZipPath.getAbsolutePath()+File.separator+zipEntry.getName();
System.out.println(zipEntry.getName());
filePath = new String(filePath.getBytes(),"gbk");
File file = new File(filePath);
File parentFile= file.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
input = zipFile.getInputStream(zipEntry);
output = new FileOutputStream(file);
byte[] buffer = new byte[8092];
int realLength = 0;
while((realLength = input.read(buffer)) != -1)
{
output.write(buffer,0,realLength);
}
}
}
zipFile.close();
output.close();
input.close();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void zip(File sourceFolder, File zipFile) {
ZipOutputStream zos = null;
try {
ZipOutputStream zos2 = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
zipFile(sourceFolder, sourceFolder.isDirectory() ? sourceFolder.getPath() : sourceFolder.getParent(), zos2);
zos2.closeEntry();
zos2.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
public static void zip(String sourceDir, String zipFile) {
zip(new File(sourceDir), new File(zipFile));
}
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
// 创建文件对象用于装载要压缩文件
File[] files = new File[0];
String pathName;
int length = 0;
// 检测是否有子目录
if (source.isDirectory()) {
// 获取目录下所有文档
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
byte[] buf = new byte[1024];
try {
for (File file : files) {
if (file.isDirectory()) {
// 获得文件路径+“/”表示还有子目录
pathName = file.getPath().substring(basePath.length())
+ "/";
// 创建一个Zip文件目录进行传递给输出流
zos.putNextEntry(new ZipEntry(pathName));
// 递归压缩到没有子目录
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length());
// 打开文件输入通道
InputStream is = new FileInputStream(file);
// 使用缓冲流对接压缩流
BufferedInputStream bis = new BufferedInputStream(is);
// 创建压缩目录
zos.putNextEntry(new ZipEntry(pathName));
// 循环写入压缩流
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
// 关闭文件流
is.close();
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
0 0
- file压缩
- File压缩ZIP文件
- springmvc MultipartFile转file和图片压缩
- jQuery实现对file对象的压缩
- File操作(8) ----------------------- 文件压缩
- android 上传图片(压缩) Bitmap 转File
- android 上传图片(压缩) Bitmap 转File
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- Centos下安装带界面压缩解压工具file-roller
- FileUtil(File处理工具类,copy,压缩,删除等)
- php file manager php文件管理 上传,移动,压缩,解压缩
- 微信端图片压缩转base64,然后转file形式上传
- HTML5 input file 图片上传,压缩,上传,预览
- 图片的等比例压缩和质量压缩,图片转化成FILE
- 压缩
- 压缩
- 压缩
- 【C语言探索之旅】 第三部分第一课:SDL开发游戏之安装SDL
- 马化腾回忆创业:曾假扮女孩子陪聊
- 为什么斗地主越来越受人喜欢
- LeetCode_5---Longest Palindromic Substring (求最长回文子串)
- break continue switch 语句的使用
- file压缩
- 多次格式化namenode之后datanode无法启动
- 利用HighCharts 显示饼图
- 黑马程序员——循环结构和方法
- Cannot resolve the collation conflict between "SQL_Latin1_Gen
- 第十三周 项目三--立体类族共有的抽象类
- 人人都是网站分析师:从分析师的视角理解网站和解读数据
- “ 戏"说 明星”与“麻将
- 面试题26:复杂链表的赋值