JAVA实现压缩文件的方法(使用java.util.zip.*)
来源:互联网 发布:拉里约翰逊数据 编辑:程序博客网 时间:2024/06/06 02:02
以下是两个摘自《java examples in a nutshell》的两个方法,挺简单的。
/** Gzip the contents of the from file and save in the to file. */
public static void gzipFile(String from, String to) throws IOException {
// Create stream to read from the from file
FileInputStream in = new FileInputStream(from);
// Create stream to compress data and write it to the to file.
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
// Copy bytes from one stream to the other
byte[ ] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1)
out.write(buffer, 0, bytes_read);
// And close the streams
in.close( );
out.close( );
}
/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile)
throws IOException, IllegalArgumentException {
// Check that the directory is a directory, and get its contents
File d = new File(dir);
if (!d.isDirectory( ))
throw new IllegalArgumentException("Compress: not a directory: " +
dir);
String[ ] entries = d.list( );
byte[ ] buffer = new byte[4096]; // Create a buffer for copying
int bytes_read;
// Create a stream to compress data and write it to the zipfile
ZipOutputStream out =
new ZipOutputStream(new FileOutputStream(zipfile));
// Loop through all entries in the directory
for(int i = 0; i < entries.length; i++) {
File f = new File(d, entries[i]);
if (f.isDirectory( )) continue; // Don't zip sub-directories
FileInputStream in = new FileInputStream(f); // Stream to read file
ZipEntry entry = new ZipEntry(f.getPath( )); // Make a ZipEntry
out.putNextEntry(entry); // Store entry
while((bytes_read = in.read(buffer)) != -1) // Copy bytes
out.write(buffer, 0, bytes_read);
in.close( ); // Close input stream
}
// When we're done with the whole loop, close the output stream
out.close( );
}
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- java实现zip压缩文件
- java实现zip压缩文件
- java实现zip压缩文件
- java实现 zip压缩文件 三种方法
- 使用java.util.zip包压缩和解压缩文件
- 使用java.util.zip包压缩和解压缩文件
- java实现zip压缩文件/文件夹
- java实现zip压缩文件 (一)
- java实现zip压缩文件/文件夹
- java实现zip压缩文件/文件夹
- 非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法
- 使用java.util.zip实现zip压缩和解压
- 用java中的java.util.zip包读取压缩文件
- 在java中实现zip压缩文件的具体实现
- Java做成Zip文件,Java实现压缩文件
- java 使用7-zip解压缩带密码的压缩文件(非Proccess方法)
- java实现将ZIP压缩文件解压的工具类
- 网页常用Javascript
- Ext combox 树的实现
- Log4j 配合 java 类的简单封装
- 关于TCP/IP知识点
- Telnet服务
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- 爬虫之事
- 图片旋转处理js
- Windows Mobile 网络连接管理
- js设置图片或文字自动左右滚动控制,鼠标经过停留
- JS prototype方法应用
- 在shell中用su命令
- 圆角头像的制作
- lucene之旅(一)