要使用以下的Class,你需要先引用 Microsoft Visual J# .NET Class Library,從引用(Add Reference)中,找到 "vjslib" 並引用它。
using java.io;
using java.util.zip;
public void CompressFile(string ZipFileName,string[] EntryFileName)
{
// Output stream
FileOutputStream fos = new FileOutputStream(ZipFileName);
// Tie to zip stream
ZipOutputStream zos = new ZipOutputStream(fos);
foreach(string strFileInput in EntryFileName)
{
// Stream with source file
FileInputStream fis = new FileInputStream(strFileInput);
// It's our entry in zip
string strFileNameWithoutPath=strFileInput;
if(strFileInput.IndexOf(@"\")!=-1) //把路徑去掉,避免壓縮好的檔案包含完整路徑
strFileNameWithoutPath=strFileInput.Substring(strFileInput.LastIndexOf(@"\")+1);
ZipEntry ze = new ZipEntry(strFileNameWithoutPath);
zos.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
// Read and write until done
while((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}
fis.close();
}
// Close everything
zos.closeEntry();
zos.close();
fos.close();
}