java中如何使用apache的ZipOutputStream进行zip(rar等压缩文件)文件压缩



java中如何使用apache的ZipOutputStream对zip(rar等压缩文件)的文件压缩,java文件压缩实例,java程序如何压缩文件测成zip或rar?JAVA自带的ZipOutputStream类有中文乱码的问题,因此我们使用apache的ZipOutputStream类。

先去Ant官网下载一个Ant,然后在项目中导入Ant.jar

直接上代码:(在import时要注意引入ant.jar中的ZipOutputStream类)

public class Test {
public static void main(String[] args) {
Test t = new Test();
try {
t.createZip(“G:\\test”, “G:\\a.rar”);
t.createFileToZip(“G:\\test\\a.html”, “G:\\b.zip”);
t.createFileToZip(new File(“G:\\test\\a.html”), new File(“G:\\c.zip”));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 将d://temp//zipout目录下的所有文件连同子目录压缩到d://temp//out.zip.
*
* @param baseDir
* 所要压缩的目录名(包含绝对路径)
* @param objFileName
* 压缩后的文件名
* @throws Exception
*/
public void createZip(String baseDir, String objFileName) throws Exception {
File folderObject = new File(baseDir);

if (folderObject.exists()) {
List<File> fileList = getSubFiles(new File(baseDir));

// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
objFileName));

ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
System.out.println(“Adding: ” + f.getPath() + f.getName());

// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());

// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
System.out.println(“done…”);
}
zos.close();
} else {
throw new Exception(“this folder isnot exist!”);
}
}

/**
* 将指定文件压缩后存到一压缩文件中
*
* @param zipFilename
* 压缩后的文件名
* @param sourceFileName
* 所要压缩的目录名
* @return 压缩后文件的大小
* @throws Exception
*/
public long createFileToZip(String zipFilename, String sourceFileName)
throws Exception {

File sourceFile = new File(sourceFileName);

byte[] buf = new byte[1024];

// 压缩文件名
File objFile = new File(zipFilename);

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

ZipEntry ze = null;
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(sourceFile.getName());
ze.setSize(sourceFile.length());
ze.setTime(sourceFile.lastModified());


// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);

InputStream is = new BufferedInputStream(
new FileInputStream(sourceFile));

int readLen = -1;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
zos.close();

return objFile.length();
}

/**
* 将指定文件压缩后存到一压缩文件中
*
* @param zipFile
* 压缩后的文件
* @param sourceFile
* 所要压缩的文件
* @return 压缩后文件的大小
* @throws Exception
*/
public long createFileToZip(File zipFile, File sourceFile)
throws IOException {

byte[] buf = new byte[1024];

// 压缩文件名
File objFile = zipFile;

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

ZipEntry ze = null;
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(sourceFile.getName());
ze.setSize(sourceFile.length());
ze.setTime(sourceFile.lastModified());

// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);

InputStream is = new BufferedInputStream(
new FileInputStream(sourceFile));

int readLen = -1;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
zos.close();

return objFile.length();
}

/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir
* File 指定的目录
* @return 包含java.io.File的List
*/
private List<File> getSubFiles(File baseDir) {
List<File> ret = new ArrayList<File>();
// File base=new File(baseDir);
File[] tmp = baseDir.listFiles();
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
ret.add(tmp[i]);
}
if (tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]));
}
}
return ret;
}

/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
* @param baseDir
* java.lang.String 根目录
* @param realFileName
* java.io.File 实际的文件名
* @return 相对文件名
*/
private String getAbsFileName(String baseDir, File realFileName) {
File real = realFileName;
File base = new File(baseDir);
String ret = real.getName();
while (true) {
real = real.getParentFile();
if (real == null)
break;
if (real.equals(base))
break;
else {
ret = real.getName() + “/” + ret;
}
}
System.out.println(“TTTTT” + ret);
return ret;
}

关于如何解压文件,使用apache的工具类进行zip(rar等压缩文件)的解压