站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题压缩文件和目录

压缩文件和目录

摘要: 压缩文件和目录

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="734" height="31" border="0"> <tr> <td> </td> </tr> </table>
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/

import java.io.*;
import java.util.zip.*;

/**
* 这个类定义了两种静态方法,一个使用gzip压缩文件
* 一个使用zip压缩目录。
**/
public class Compress {
/** 用Gzip压缩文件的内容并存入文件 */
public static void gzipFile(String from, String to) throws IOException {
// Create stream to read from the from file
FileInputStream in = new FileInputStream(from);
// 创建GZIP输出流来压缩数据并写入to文件
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
// 从一个流复制数据到另一个流
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压缩目录的内容并存入到zip文件 */
public static void zipDirectory(String dir, String zipfile)throws IOException, IllegalArgumentException {
// 检测是否是目录,获取其内容
File d = new File(dir);
if (!d.isDirectory())
throw new IllegalArgumentException("Compress: not a directory: " +dir);
String[] entries = d.list();//获取dir目录下所有目录和文件
byte[] buffer = new byte[4096]; // 创建复制缓冲区
int bytes_read;

//创建一个zip输出流来压缩数据并写入到zip文件
ZipOutputStream out =new ZipOutputStream(new FileOutputStream(zipfile));

// 遍历目录所有项
for(int i = 0; i < entries.length; i++) {
File f = new File(d, entries[i]);
if (f.isDirectory()) continue; // 不处理子目录
FileInputStream in = new FileInputStream(f); // 创建一个文件输入流
ZipEntry entry = new ZipEntry(f.getPath()); // 做一个ZipEntry
out.putNextEntry(entry); // 存储项信息到压缩文件
while((bytes_read = in.read(buffer)) != -1) // 复制字节到压缩文件
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();
}

/**
* 测试代码
**/
public static class Test {
/**
* 压缩一个指定的文件或目录. If no destination name is
* specified, append .gz to a file name or .zip to a directory name
**/
public static void main(String args[]) throws IOException {
if ((args.length != 1)&& (args.length != 2)) { // check arguments
System.err.println("Usage: java Compress$Test []");
System.exit(0);
}
String from = args[0], to;
File f = new File(from);
boolean directory = f.isDirectory(); // 是文件还是目录?
if (args.length == 2) to = args[1];
else {
if (directory) to = from + ".zip"; // 如果是目录,用.zip后缀
else to = from + ".gz"; // 如果是文件,用.gz后缀
}

if ((new File(to)).exists()) { // 如果目的文件已存在,不覆盖它,退出.
System.err.println("Compress: won't overwrite existing file: "+to);
System.exit(0);
}

// 最后,调用上面方法之一压缩
if (directory) Compress.zipDirectory(from, to);
else Compress.gzipFile(from, to);
}
}
}


运行:

C:\java>java Compress$Test Compress.java 1.zip

C:\java></td> </tr> <tr>


↑返回目录
前一篇: Echo程序
后一篇: 在一个字符串中去掉空白和换行符