JSP上面实现目录压缩

js|压缩

zip方法 zipPath参数为保存zip的文件路径  srcPath参数为需要压缩的目录   在linux window上面测试无问题!主要是编码问题比较麻烦~要是有其他异常 请留言 或者 有什么更好的方法 欢迎给更多的意见

//zip zhe folder
void zip(String zipPath, String srcPath,javax.servlet.jsp.JspWriter out) throws Exception {
    FileOutputStream output = null;
    ZipOutputStream zipOutput = null;
    try{
        output = new FileOutputStream(zipPath);
        zipOutput = new ZipOutputStream(output);
        zipEntry(zipOutput,srcPath,srcPath,zipPath);
    }catch(Exception e){
        out.print("file zip error");
    }finally{
        if(zipOutput!=null)zipOutput.close();
    }
    out.print("zip ok"+zipPath);
}
//add the zip entry
void zipEntry(ZipOutputStream zipOs, String initPath,String filePath,String zipPath) throws Exception {
    String entryName = filePath;
    File f = new File(filePath);
    if (f.isDirectory()){// ??
        String[] files = f.list();
        for(int i = 0; i < files.length; i++)
            zipEntry(zipOs, initPath, filePath + File.separator + files[i],zipPath);
        return;
    }
    String chPh = initPath.substring(initPath.lastIndexOf("/") + 1);// ?????
    int idx=initPath.lastIndexOf(chPh);
    if (idx != -1) {
        entryName = filePath.substring(idx);
    }
    ZipEntry entry;
    entry = new ZipEntry(entryName);
    File ff = new File(filePath);
    if(ff.getAbsolutePath().equals(zipPath))return;
    entry.setSize(ff.length());
    entry.setTime(ff.lastModified());
    //the CRC efficacy 
    entry.setCrc(0);
    CRC32 crc = new CRC32();
    crc.reset();
    zipOs.putNextEntry(entry);
    int len = 0;
    byte[] buffer = new byte[2048];
    int bufferLen = 2048;
    FileInputStream input =null;
    try{
        input = new FileInputStream(filePath);
        while ((len = input.read(buffer, 0, bufferLen)) != -1) {
                zipOs.write(buffer, 0, len);
                crc.update(buffer, 0, len);
        }
    }catch(Exception e){
    }finally{
        if(input!=null)input.close();
    }
    entry.setCrc(crc.getValue());

时间: 2024-09-07 01:30:01

JSP上面实现目录压缩的相关文章

web inf-关于jsp中work目录,和web-inf下的classes目录的问题

问题描述 关于jsp中work目录,和web-inf下的classes目录的问题 如果写的是jsp代码,编译后是放到work目录里: 但是如果写的是servlet,保存到classes目录里,就不会出现在work目录里. 请问这是怎么回事呢??

Compact 显示和更改 NTFS 分区上的文件或目录压缩_DOS/BAT

Compact 显示和更改 NTFS 分区上的文件或目录压缩.如果在没有参数的情况下使用,则 compact 显示当前目录的压缩状态. 语法 compact [{/c|/u}] [/s[:dir]] [/a] [/i] [/f] [/q] [FileName[...]] 参数 /c  压缩指定的目录或文件.  /u  解压缩指定的目录或文件.  s:dir  指定将所请求的操作(压缩或解压缩)应用到指定目录的所有子目录,或者如果没有指定目录,则应用到当前目录的所有子目录. /a  显示隐藏或系统

eclipse ee jsp 动态网站目录结构

问题描述 eclipse ee jsp 动态网站目录结构 新学jsp 求讲解一下目录结构 还有 javabean文件和servlet 文件应该放在哪个目录下 新人求教~ 解决方案 http://blog.sina.com.cn/s/blog_4758a28b0100l3lp.htmlhttp://my.oschina.net/kzhou/blog/108971

jsp 查找遍历目录下所有文件

jsp教程 查找遍历目录下所有文件,我们先利用了application.getRealPath读取根目录文件,然后再利用 new file读取目录文件再保存到数组,最后用for遍历输出. <%@ page import="java.io.*" %> <HTML>     <HEAD>         <TITLE>Index of Files</TITLE>     </HEAD>     <BODY>

怎样设置 JSP 的虚拟目录

ASP #:如果你装的是tomcat3.2可以这样修改 在你的和下面的一段代码相似的地方添加如下代码 <Context path="/web"                  docBase="d:/www/jsp"                  crossContext="true"                 debug="0"                  reloadable="true&

为什么要把jsp放在WEB-INF目录下

  发现问题经常有人把jsp放在WebRoot根下,但是这样做带来一下问题. 如果用户知道某个页面的页面名称,如a.jsp,完全可以通过"项目名/a.jsp"直接访问该页面.如果该页面需要一些初始化工作,那么这个时候就会出错.   解决问题将jsp放在WebRoot/WEB-INF下.这样用户就无法直接采用项目名/a.jsp"直接访问该页面. 我们可以在Struts或者SpringMVC中操作Controller来导航该页面.  

如何在JSP中处理虚拟目录

<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*"%> <html> <head> <title>JSP中如何处理虚拟目录</title> </head> <body> 取得虚拟目录对应的磁盘路径<br> Web站点主目录的位置为<font color

linux中tar 压缩排除目录与文件方法

例1. 如我要压缩 /opt/apache/htdocs 这个文件夹 ,但是不将里面的图片文件夹uploads也压缩了. 可以用tar命令的--exclude 排除掉. 方法 压缩所有目录  代码如下 复制代码 tar -zcvf htdocs.tar.gz  /opt/apache/htdocs 排除一个目录  代码如下 复制代码 tar -zcvf htdocs.tar.gz  --exclude=/opt/apache/htdocs/uploads  /opt/apache/htdocs

jsp显示目录下所有文件与子目录代码

jsp教程显示目录下所有文件与子目录代码  public static void getFiles(List<File> fileList, String path, String fileSuffix) {           File file = new File(path);           File[] files = file.listFiles();           if (files == null) {               return;