压缩和解压缩文件
2004-10-08 09:46
330 查看
/**
* 压缩和解压缩文件
*/
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class Zip {
public Zip(){}
/**
* 压缩目录下的文件
* @param zipFileName
* @param inputFile 目录
* @throws java.lang.Exception
*/
public void zip(String zipFileName, String inputFile) throws Exception {
zip(zipFileName, new File(inputFile));
}
/**
* 压缩文件
* @param zipFileName
* @param inputFile 文件
* @throws java.lang.Exception
*/
public void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("压缩成功!");
out.close();
}
public void zip(ZipOutputStream out, File f, String base) throws Exception {
System.out.println("正在压缩 " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}
else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1)
out.write(b);
in.close();
}
}
/**
* 解压缩
* @param zipFileName
* @param outputDirectory 输出目录
* @throws java.lang.Exception
*/
public void unzip(String zipFileName, String outputDirectory) throws
Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ( (z = in.getNextEntry()) != null) {
System.out.println("正在解压 " + z.getName());
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("创建目录 " + outputDirectory + File.separator + name);
}
else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ( (b = in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
}
public static void main(String[] args) {
try {
Zip t = new Zip();
// t.zip("D://IdeaProjects.zip", "D://IdeaProjects");
// t.unzip("d://ddd//api.jar", "d://ddd//");
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
* 压缩和解压缩文件
*/
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class Zip {
public Zip(){}
/**
* 压缩目录下的文件
* @param zipFileName
* @param inputFile 目录
* @throws java.lang.Exception
*/
public void zip(String zipFileName, String inputFile) throws Exception {
zip(zipFileName, new File(inputFile));
}
/**
* 压缩文件
* @param zipFileName
* @param inputFile 文件
* @throws java.lang.Exception
*/
public void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("压缩成功!");
out.close();
}
public void zip(ZipOutputStream out, File f, String base) throws Exception {
System.out.println("正在压缩 " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}
else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1)
out.write(b);
in.close();
}
}
/**
* 解压缩
* @param zipFileName
* @param outputDirectory 输出目录
* @throws java.lang.Exception
*/
public void unzip(String zipFileName, String outputDirectory) throws
Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ( (z = in.getNextEntry()) != null) {
System.out.println("正在解压 " + z.getName());
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("创建目录 " + outputDirectory + File.separator + name);
}
else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ( (b = in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
}
public static void main(String[] args) {
try {
Zip t = new Zip();
// t.zip("D://IdeaProjects.zip", "D://IdeaProjects");
// t.unzip("d://ddd//api.jar", "d://ddd//");
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
相关文章推荐
- 使用C#压缩/解压缩7-zip文件
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密
- java解压缩/压缩/加密压缩/加密解压缩 ZIP4J---ZIP文件压缩与解压缩学习
- PB8调用使用C++与C#分别编写生成解压缩带有密码的zip压缩文件的动态链接库dll(部分内容转自互联网)
- .NET GZip压缩和解压缩文件
- linux下各种文件格式的压缩以及解压缩命令(长期更新)
- 文件操作。简单的压缩、解压缩等
- 在C#中利用SharpZipLib进行文件的压缩和解压缩(转)
- java解压缩文件和不解压读取压缩文件中的文本文件内容
- linux tar (打包.压缩.解压缩)命令说明 | tar如何解压文件到指定的目录?
- 使用java.util.zip包压缩和解压缩文件
- Linux中文件的压缩与解压缩命令操作示例集锦
- 文件 "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\mastlog.ldf " 已压缩,但未驻留在只读数据库或文件组中。必须将此文件解压缩。【转】
- 磁盘挂载与文件压缩解压缩的使用
- 在C#中压缩解压缩文件(适合.Net1.x)
- windows mobile 上文件压缩与解压缩(使用ICSharpCode.SharpZipLib.Zip)
- windows mobile下文件的压缩与解压缩
- 关于文件压缩与解压缩的那些事zipArchive
- C#中DES加密解密问题、压缩解压缩文件、进度条以及WinForm中使用控制台的解决方案
- C# 利用WinRAR (加密)压缩及解压缩 相关文件夹及文件