1. 把文件生成到tomcat目录下,这种写法对磁盘读写太大,而且时间比较慢
3. 优化读取速度
private void downloadAllLogs(HttpServletRequest request, HttpServletResponse response)
{
String fileName = request.getParameter("fileName");
String rootPath = "";
String fileSeparator = "";
String logPath = rootPath + fileSeparator + "logs\\";
ZipOutputStream zos = null;
String newFileName = fileName + ".zip";
try
{
zos = new ZipOutputStream(new FileOutputStream(newFileName));
Zip(logPath, logPath.lastIndexOf("\\"), zos);
zos.closeEntry();
zos.close();
}
catch (FileNotFoundException e)
{
throw new UnsupportedOperationException("FileNotFoundException Exception", e);
}
catch (IOException e)
{
throw new UnsupportedOperationException("IOException Exception", e);
}
}
<pre name="code" class="java">private void Zip(String path, int baseIndex, ZipOutputStream out) throws IOException
{
File file = new File(path);
File[] files = null;
if(file.isDirectory())
{
files = file.listFiles();
}
else
{
files = new File[1];
files[0] = file;
}
for(File f : files)
{
if(f.isDirectory())
{
Zip(f.getPath(), baseIndex, out);
}
else
{
String pathName = f.getPath().substring(baseIndex + 1);
ZipEntry ze = new ZipEntry(pathName);
ze.setSize(f.length());
ze.setTime(f.lastModified());
out.putNextEntry(ze);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
int c;
while((c = bis.read()) != -1)
{
out.write(c);
}
bis.close();
}
}
}
2. 增加内存,不对磁盘进行读写,直接生成文件; 但是如果文件数量大,内容多的话,读取速度太慢
private void downloadAllLogs(HttpServletRequest request, HttpServletResponse response)
{
String fileName = request.getParameter("fileName");
String rootPath = "";
String fileSeparator = "";
String logPath = rootPath + fileSeparator + "logs\\";
ZipOutputStream zos = null;
ByteArrayOutputStream baos = null;
try
{
baos = new ByteArrayOutputStream();
zos = new ZipOutputStream(baos);
Zip(logPath, logPath.lastIndexOf("\\"), zos);
zos.closeEntry();
zos.flush();
zos.close();
byte[] tba = baos.toByteArray();
baos.flush();
baos.close();
response.reset();
response.setContentType(RequestHandler.DEFAULT_CONTENT_TYPE);
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
long start = System.currentTimeMillis();
ServletOutputStream servletOS = response.getOutputStream();
servletOS.write(tba);
servletOS.flush();
servletOS.close();
}
catch (FileNotFoundException e)
{
throw new UnsupportedOperationException("FileNotFoundException Exception", e);
}
catch (IOException e)
{
throw new UnsupportedOperationException("IOException Exception", e);
}
}
<pre name="code" class="java">private void Zip(String path, int baseIndex, ZipOutputStream out) throws IOException
{
long start = System.currentTimeMillis();
System.out.println("循环压缩文件执行时间start:" + start);
File file = new File(path);
File[] files = null;
if(file.isDirectory())
{
files = file.listFiles();
}
else
{
files = new File[1];
files[0] = file;
}
for(File f : files)
{
String pathName = f.getPath().substring(baseIndex + 1);
out.putNextEntry(new ZipEntry(pathName));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
int readLine = 0;
int bufferSize = (int) f.length();
byte[] buffer = new byte[bufferSize];
while((readLine = bis.read(buffer, 0 , bufferSize)) != -1)
{
out.write(buffer, 0, readLine);
}
bis.close();
}
long end = System.currentTimeMillis() - start;
System.out.println("循环压缩文件执行时间end:" + end);
}
3. 优化读取速度