客户端使用apache commons compress压缩和下载文件时中止异常。文件大小巨大(压缩后约为400 MB)。一段时间后,下载突然停止,并且在下载栏中显示的消息是(网络失败)。
我的代码从.txt文件中读取要压缩的文件列表的路径并将其压缩。代码如下。
文件复制逻辑
sTxtFilePath = // txt文件的路径
#include <iostream>
#include <string>
using namespace std;
int maxNum;
int num[20];
int main()
{
cout << "Enter an Array" << endl;
for (int i=0;i<20;i++)
{
cin >> num[i];
maxNum++;
if (num[i]==-1)
break;
}
cout <<"Your array is: " << endl;
for (int i=0;i<maxNum-1;i++)
cout << num[i];
cout << endl;
cout << "Your doubled array is:" << endl;
for (int j=0;j<maxNum-1;j++)
{
num[j]*=2;
cout << num[j];
}
cout << endl;
cout << "When the digits of each seat are added..." << endl;
for (int k=0;k<maxNum;k++)
{
for (int l=0;l<maxNum;l++)
{
int sum[20];
while (num[k]!=0)
{
sum[l]=sum[l]+num[k]%10;
num[k]=num[k]/10;
}
}
cout << sum[l];
}
cout << endl;
}
压缩逻辑
txtFile=new File(sTxtFilePath);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"" + sDirectory + ".zip\"");
out = response.getOutputStream();
File zipFile = new File(sDirectory + ".zip");
zipOutputStream = new ZipArchiveOutputStream(zipFile);
Zip zip = new ApacheZipImpl();
if (txtFile.exists())
{
in = new BufferedReader(new FileReader(txtFile));
while ((sFilePath = in.readLine()) != null)
{
filePath = new File(sFilePath);
sbFilePath=new StringBuffer();
if(filePath.getCanonicalPath().contains("/")){
basePath=filePath.getCanonicalPath().split("/");
}
else{
basePath=filePath.getCanonicalPath().split("\\\\");
}
for(int i=3;i<basePath.length;i++){
sbFilePath.append(basePath[i]);
sbFilePath.append(File.separator);
}
if (filePath.isDirectory())
{
zip.writeToZipOutputStream(zipOutputStream, filePath,sbFilePath.toString(),true);
}
else
{
zip.writeToZipOutputStream(zipOutputStream, filePath,sbFilePath.toString().substring(0,sbFilePath.toString().length()-1));
}
//zipOutputStream.flush();
}
}
zipOutputStream.close();
response.setHeader("Content-Length", Long.toString(zipFile.length()));
IOUtils.copy(new FileInputStream(sDirectory+".zip"), out);
out.close();