【发布时间】:2014-07-01 23:35:12
【问题描述】:
我有以下从 mkyong 获得的代码,用于压缩本地文件。但是,我的要求是在服务器上压缩文件并需要下载它。谁能帮忙。
将代码写入 zipFiles:
public void zipFiles(File contentFile, File navFile)
{
byte[] buffer = new byte[1024];
try{
// i dont have idea on what to give here in fileoutputstream
FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry(contentFile.toString());
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(contentFile.toString());
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
我可以在文件输出流中提供什么? contentfile 和 navigationfile 是我用代码创建的文件。
【问题讨论】:
-
请澄清您的问题。此代码打算在服务器或客户端上运行吗?如果是服务器,什么样的服务器?以 Servlet 为例?
-
实现一个服务器发送数据,一个客户端接收数据?或者使用当前流行的协议,如 ssh 和 ftp 等。