java下载多级文件夹以及文件压缩下载

/**
 * 写入压缩包
 * file
 * parentPath
 */
public void writeZip(File file, String parentPath, ZipOutputStream zos) {
    if (file.exists()) { // 需要压缩的文件夹是否存在
    	
        if (file.isDirectory()) {// 是文件夹
    
			parentPath += file.getName() + File.separator; // 文件夹名称 + "/"

            File[] files = file.listFiles(); // 获取文件夹下的文件夹或文件
            if (files.length != 0) {
                for (File f : files) {
                    writeZip(f, parentPath, zos);
                }
            } else { // 空目录则创建当前目录
                try {
                    zos.putNextEntry(new ZipEntry(parentPath));
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                } 
            }
        } else { //是文件
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                
    			nameStr = parentPath + file.getName();
    			
                ZipEntry ze = new ZipEntry(nameStr);
                zos.putNextEntry(ze);
                
                byte[] content = new byte[1024];
                int len;
                while ((len = fis.read(content)) != -1) {
                    zos.write(content, 0, len);
                    zos.flush();
                }
                
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            } 
        }
    }

}



/**
 * @param request
 * @param response
 * 下载目录  
 */
@RequestMapping("/doc/DocAction!downLoadFolderZip.action")
public void downLoadFolderZip(HttpServletRequest request, HttpServletResponse response){
	String zipName = "";// 定义压缩包名称
    FileOutputStream fos = null;
    ZipOutputStream zos = null;
    OutputStream out = null;
	try{
		
		String docId = request.getParameter("docId");
		
		//查询当前要下载的目录,这里是我的业务数据doc,通过doc拿到下载路径
		DocInfoVo vo = new DocInfoVo();
		vo.setDocId(docId);
		DocInfoVo doc = docInfoService.queryDocInfoVo(vo);
		
		//下载地址
		String configPath = ReadConfigUtils.getConfigValueUpFilePath("sharedoc")+"/"; //这是配置文件配置的地址
		String docPath = doc.getMuluIdPath().replaceAll("\\|", Matcher.quoteReplacement(File.separator));//转义一下,目录在磁盘的位置
		
		if(StringUtils.isNotBlank(configPath) && StringUtils.isNotBlank(docPath)){
			zipName = doc.getDocName()+".zip";//下载的zip名称
			File zipFile = new File(zipName);
			
			try {
	            File files = new File(configPath + docPath);
	            fos = new FileOutputStream(zipName);
	            zos = new ZipOutputStream(fos);
	            
	            writeZip(files, "", zos);
	 
	        } catch (FileNotFoundException e) {
	            logger.error(e.getMessage(), e);
	        } finally {
	        	if(zos != null){
	        		zos.close();//这里关闭ZipOutputStream,不会出现解压文件文件末端出错的问题
	        	}
	        }
			
	        try {
	            out = response.getOutputStream();
	            response.reset();
	            response.setHeader("Content-Disposition", "attachment;filename="
	                    + new String(zipName.getBytes("GB2312"), "ISO-8859-1"));
	            response.setContentType("application/octet-stream; charset=utf-8");
	            response.setCharacterEncoding("UTF-8");
	            out.write(FileUtils.readFileToByteArray(zipFile));
	            out.flush();
	            
	        } catch (IOException e) {
	            logger.error(e.getMessage(), e);
	        }finally{
	        	if(fos != null){
	        		fos.close();
	        	}
	        	if(out != null){
	        		out.close();
	        	}
	        }
		}
	}catch(Exception e){
		logger.error(e.getMessage(),e);
	}
}

注明:部分代码是从其他博客那复制的,原文链接忘了,请见谅;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt C++ 中删除多级文件夹包括文件可以使用QDir类和QFile类。首先,我们可以使用QDir::removeRecursively()函数来删除整个目录,包括所有子目录和文件。然后,我们可以使用QFile::remove()函数删除单个文件。 以下是一个示例代码,可以删除多级文件夹包括文件: ```cpp #include <QCoreApplication> #include <QDir> #include <QFile> #include <QDebug> void deleteFolder(const QString& folderPath) { QDir dir(folderPath); // 判断文件夹是否存在 if (!dir.exists()) { return; } // 获取文件夹内所有子目录和文件 QFileInfoList fileList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); // 遍历所有子目录和文件 foreach (QFileInfo fileInfo, fileList) { if (fileInfo.isDir()) { // 如果是目录,则递归删除 deleteFolder(fileInfo.filePath()); } else { // 如果是文件,则直接删除 QFile::remove(fileInfo.filePath()); } } // 删除目录 if (dir.rmdir(folderPath)) { qDebug() << "Folder deleted successfully!"; } else { qDebug() << "Failed to delete folder!"; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString folderPath = "C:/TestFolder"; // 要删除的文件夹路径 deleteFolder(folderPath); return a.exec(); } ``` 在这个示例中,我们首先使用QDir类获取文件夹内所有子目录和文件,然后遍历所有子目录和文件,如果是目录则递归删除,如果是文件则直接删除。最后,我们使用QDir::rmdir()函数删除目录本身。 请注意,删除操作是不可逆的,因此在使用此函数之前,请确定您已经备份了需要保留的文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值