📄 zipdownload.java
字号:
package com.sslexplorer.vfs;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.AbstractDownloadContent;
import com.sslexplorer.vfs.webdav.DAVResource;
import com.sslexplorer.vfs.webdav.DAVServlet;
import com.sslexplorer.vfs.webdav.DAVUtilities;
import com.sslexplorer.vfs.webdav.FileObjectDAVResource;
/**
* Implementation of {@link com.sslexplorer.core.AbstractDownloadContent} for
* downloading an of VFS uri's.
* <p>
* The download will be presented as a zip archive with a filename of either the
* only URI select or of the basename of the root folder (both suffixed with
* .zip).
*
* @author James D Robinson <a href="mailto:james@3sp.com"><james@3sp.com></a>
*
*/
public class ZipDownload extends AbstractDownloadContent {
private String[] uris;
private String rootPath;
private ActionForward messageForward;
private int downloadCount;
/**
* @param messageForward forward to use to display message
* @param rootPath The current location.
* @param uris The Array of source uri's
* @param forward The forward to go to after the operation.
* @param messageKey The message key
* @param messageResourcesKey The message resource key.
*/
public ZipDownload(ActionForward messageForward, String rootPath, String[] uris, ActionForward forward, String messageKey,
String messageResourcesKey) {
super("application/x-zip", forward, messageKey, messageResourcesKey, null, null, null, null, null, false);
this.uris = uris;
this.rootPath = rootPath;
this.messageForward = messageForward;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.DownloadContent#sendDownload(javax.servlet.http.HttpServletResponse,
* javax.servlet.http.HttpServletRequest)
*/
public void sendDownload(HttpServletResponse response, HttpServletRequest request) throws Exception {
OutputStream out = null;
try {
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < uris.length; i++) {
DAVResource res = DAVServlet.getDAVResource(request, response, rootPath + "/" + uris[i]);
zipit(zos, res, "");
}
zos.close();
} catch (IOException ioe) {
throw ioe;
} finally {
downloadCount++;
Util.closeStream(out);
}
}
/**
* @param zos The ZipOutputStream going to the destination.
* @param res The DAVResource to zip
* @param path The path after the current location.
* @throws IOException
*/
private void zipit(ZipOutputStream zos, DAVResource res, String path) throws Exception {
if (res.isCollection()) {
Iterator children = res.getChildren();
while (children.hasNext()) {
FileObjectDAVResource childResource = (FileObjectDAVResource) children.next();
zipit(zos, childResource, DAVUtilities.concatenatePaths(path, res.getDisplayName()));
}
} else {
String zipPath = DAVUtilities.stripLeadingSlash(DAVUtilities.concatenatePaths(path, res.getDisplayName()));
ZipEntry idEntry = new ZipEntry(zipPath);
zos.putNextEntry(idEntry);
InputStream in = res.read();
try {
byte[] buf = new byte[4096];
int read;
while (true) {
read = in.read(buf, 0, buf.length);
if (read == -1) {
break;
}
zos.write(buf, 0, read);
}
} finally {
zos.closeEntry();
in.close();
}
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.DownloadContent#getFilename()
*/
public String getFilename() {
return (uris.length == 1 ? DAVUtilities.stripTrailingSlash(uris[0]) : DAVUtilities.basename(rootPath, '/')) + ".zip";
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.DownloadContent#getMessageForward()
*/
public ActionForward getMessageForward() {
return messageForward;
}
public int getDownloadCount() {
return downloadCount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -