Java:ZipOutputString include sub directory

/**
* Filename: Rt.java
* Description:
* Copyright: Copyright (c)2008
* @author: darrenyuan
* @version: 1.0
* Create at: 2012-4-18 上午12:24:25
*
* Modification History:
* Date Author Version Description
* ------------------------------------------------------------------
* 2012-4-18 darrenyuan 1.0 1.0 Version
*/
package learnByInternet;
import java.io.*;
import java.util.zip.*;
/**
* @author Administrator
*
*/
public class ZipUsingJavaUtil
{
/*
* Zip function zip all files and folders
*/

public boolean zipFiles(String srcFolder, String destZipFile)
{
boolean result=false;
try
{
System.out.println("Program Start zipping the given files");
/*
* send to the zip procedure
*/
zipFolder(srcFolder,destZipFile);
result=true;
System.out.println("Given files are successfully zipped");
}
catch(Exception e)
{
System.out.println("Some Errors happned during the zip process");
}
finally
{
return result;
}
}
/*
* zip the folders
*/
private void zipFolder(String srcFolder, String destZipFile) throws Exception
{
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
/*
* create the output stream to zip file result
*/
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
/*
* add the folder to the zip
*/
addFolderToZip("", srcFolder, zip);
/*
* close the zip objects
*/
zip.flush();
zip.close();
}
/*
* recursively add files to the zip files
*/
private void addFileToZip(String path, String srcFile, ZipOutputStream zip,boolean flag)throws Exception
{
/*
* create the file object for inputs
*/
File folder = new File(srcFile);

/*
* if the folder is empty add empty folder to the Zip file
*/
if (flag==true)
{
zip.putNextEntry(new ZipEntry(path + "/" +folder.getName() + "/"));
}
else
{ /*
* if the current name is directory, recursively traverse it to get the files
*/
if (folder.isDirectory() )
{
/*
* if folder is not empty
*/
addFolderToZip(path, srcFile, zip);
}
else
{
/*
* write the file to the output
*/
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0)
{
/*
* Write the Result
*/
zip.write(buf, 0, len);
}
}
}
}
/*
* add folder to the zip file
*/
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
throws Exception
{
File folder = new File(srcFolder);

/*
* check the empty folder
*/
if (folder.list().length == 0)
{
System.out.println(folder.getName());
addFileToZip(path , srcFolder, zip,true);
}
else
{
/*
* list the files in the folder
*/
for (String fileName : folder.list())
{
if (path.equals(""))
{
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip,false);
}
else
{
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip,false);
}
}
}
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值