zip4j加密压缩输出流,浏览器下载


import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import net.lingala.zip4j.io.ZipOutputStream;

import java.io.File;
import java.util.ArrayList;

 
public void encryptZipDownload(HttpServletResponse response,ArrayList<File> files,
    String fileName,String password) {
 		ZipOutputStream outputStream = null;
		InputStream inputStream = null;
		try {
 			response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getByte("UTF-8"),"iso-8859-1"));

			outputStream = new ZipOutputStream(response.getOutputStream());
			
			ZipParameters parameters = new ZipParameters();
 			parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
			parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
			parameters.setEncryptFiles(true);
			parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
			parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
			parameters.setPassword(password);
			
 			for (File f:files) {
				outputStream.putNextEntry(f,parameters);
 				inputStream = new FileInputStream(f);
				byte[] readBuff = new byte[4096];
				int readLen = -1;
				
 				while ((readLen = inputStream.read(readBuff)) != -1) {
					outputStream.write(readBuff, 0, readLen);
				}
				outputStream.closeEntry();
				inputStream.close();
			}
			outputStream.finish();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用以下代码将文件压缩zip下载浏览器: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <dirent.h> #include <time.h> #include <utime.h> #include <zlib.h> #define CHUNK 16384 int compress_file(const char *src, const char *dst) { int ret; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; FILE *source = NULL; FILE *dest = NULL; source = fopen(src, "rb"); if (source == NULL) { fprintf(stderr, "Failed to open file %s: %s\n", src, strerror(errno)); return -1; } dest = fopen(dst, "wb"); if (dest == NULL) { fprintf(stderr, "Failed to open file %s: %s\n", dst, strerror(errno)); fclose(source); return -1; } strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); if (ret != Z_OK) { fprintf(stderr, "Failed to initialize zlib: %s\n", strm.msg); fclose(source); fclose(dest); return -1; } do { strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { fprintf(stderr, "Failed to read file %s: %s\n", src, strerror(errno)); deflateEnd(&strm); fclose(source); fclose(dest); return -1; } if (strm.avail_in == 0) break; strm.next_in = in; do { strm.avail_out = CHUNK; strm.next_out = out; ret = deflate(&strm, Z_FINISH); if (ret == Z_STREAM_ERROR) { fprintf(stderr, "Failed to compress file %s: %s\n", src, strm.msg); deflateEnd(&strm); fclose(source); fclose(dest); return -1; } have = CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { fprintf(stderr, "Failed to write compressed data to file %s: %s\n", dst, strerror(errno)); deflateEnd(&strm); fclose(source); fclose(dest); return -1; } } while (strm.avail_out == 0); } while (ret != Z_STREAM_END); deflateEnd(&strm); fclose(source); fclose(dest); return 0; } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source file> <destination file>\n", argv[0]); return 1; } if (compress_file(argv[1], argv[2]) != 0) { fprintf(stderr, "Failed to compress file %s\n", argv[1]); return 1; } return 0; } ``` 您可以将源文件和目标文件的路径作为命令行参数传递给该程序。例如,如果您要将文件`/path/to/file.txt`压缩成`/path/to/file.zip`,则可以运行以下命令: ``` $ ./compress /path/to/file.txt /path/to/file.zip ``` 该程序将使用zlib库将文件压缩zip格式,并将压缩后的文件保存到目标文件中。您可以在浏览器下载该文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值