Java 编程之实现文件或目录的压缩.
来源:互联网 发布:jquery 对象数组 查找 编辑:程序博客网 时间:2024/04/28 10:01
package cn.itcast;
import java.util.zip.*;
import java.io.*;
public class CreateZipFile {
private static void DirectoryZip(ZipOutputStream out, File f, String base) throws Exception
{
if (f.isDirectory())
{
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base+"/"));
if(base.length()==0)
{
base="";
}
else
{
base=base+"/";
}
for (int i = 0; i < fl.length; i++)
{
DirectoryZip(out, fl[i], base + fl[i].getName());
}
}
else{
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1)
{
out.write(b);
}
in.close();
}
}
public static void FileZip(ZipOutputStream zos,File file) throws Exception
{
if(file.isFile())
{
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis=new FileInputStream(file);
int buf=0;
while((buf=fis.read())!=-1)
{
zos.write(buf);
}
fis.close();
System.out.println(file.getName());
}
else
{
DirectoryZip(zos,file,"");
}
}
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
long start = System.currentTimeMillis();
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(args[1]));
FileZip(zos,new File(args[0]));
zos.close();
long end = System.currentTimeMillis();
System.out.println('/n'+""+'/n'+"耗用时间(秒) : "+(end-start)/1000+" "+'/n'+ " -----恭喜您! 压缩完毕!!!");
}
}
此程序接收接受两个参数 第一个是你要压缩的文件或目录的路径,第二个是你要把压缩的文件放到什么地方的路径 由于java.util.zip包对中文目录问题的不支持 所以本程序不能压缩中文目录和文件,如果想解决这个问题的话可以去apache去下载一个名为org.apache.tools.zip 包 可以解决这个中文目录名问题....
- Java 编程之实现文件或目录的压缩.
- Java 编程之实现文件或目录的压缩.
- Java 编程之实现文件或目录的解压.
- Java 编程之实现文件或目录的解压.
- Java 编程之实现文件或目录的解压.
- Java 编程之实现文件或目录的解压.
- java实现文件目录压缩
- Java压缩(文件或目录)
- java实现将文件或文件夹压缩
- 利用SharpZipLib实现压缩多个文件或整个目录
- SharpZipLib实现压缩多个文件或整个目录
- Java之读取包或resource目录下面的文件
- java压缩文件夹或文件的方法
- Java压缩目录的简单实现
- JAVA实现将一个目录里的文件压缩到zip文件里
- WinRAR压缩时排除文件或目录
- Java实现Zip压缩/解压缩目录中的所有文件
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- 深夜的感触
- OpenCms JSP 模板开发——创建一个简单的JSP模板
- 输入输出系统
- Ruby Gems, Python Eggs, and the beauty of unittest
- 74LS273与74LS373的区别
- Java 编程之实现文件或目录的压缩.
- 计算机科学大牛
- 关于接口和抽象类
- 泛型算法摘要
- Java 编程之实现文件或目录的解压.
- c#.net常用的小函数和方法集
- C++ class中的静态(static)成员
- Monte Carlo Simulation technique
- 进程中有多个iexplore.exe