欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现 上传压缩包,解压,实现打zip压缩包

程序员文章站 2022-06-28 16:36:09
1、打压缩包@RequestMapping(value = “/exportAllDutyFiles”, method = RequestMethod.GET)@ResponseBodypublic void exportAllDutyFiles(HttpServletResponse response, HttpServletRequest request) {File sourceFile=new File(uploadPerformance);if(!sourceFile.exists())...

1、打压缩包
@RequestMapping(value = “/exportAllDutyFiles”, method = RequestMethod.GET)
@ResponseBody
public void exportAllDutyFiles(HttpServletResponse response, HttpServletRequest request) {
File sourceFile=new File(uploadPerformance);
if(!sourceFile.exists()){
throw new RuntimeException(uploadPerformance + “不存在!”);
}
String path=uploadPerformance +".zip"; //压缩包路径名称
File targetFile=new File(path);
List fileList=new ArrayList();
fileList.addAll(Arrays.asList(sourceFile.listFiles()));
ExcelUtil.zipFiles(fileList,targetFile);
downloadFile(targetFile,response,true);
}

/**
* 将多个Excel打包成zip文件
* @param srcfile
* @param zipfile
*/
public static void zipFiles(List srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.size(); i++) {
File file = srcfile.get(i);
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void downloadFile(File file,HttpServletResponse response,boolean isDelete) {
	try {
		// 以流的形式下载文件。
		BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		fis.close();
		// 清空response
		response.reset();
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
		toClient.write(buffer);
		toClient.flush();
		toClient.close();
		if(isDelete)
		{
			file.delete();        //是否将生成的服务器端文件删除
		}
	}
	catch (IOException ex) {
		ex.printStackTrace();
	}

}

上传压缩包
@RequestMapping("/importZipData")
@ResponseBody
public JSONObject importZipData(MultipartFile file) {
JSONObject json = new JSONObject();
json.put(“status”, “200”);
json.put(“message”, “上传成功”);
// 判断源文件是否存在
if (file == null) {
json.put(“status”, “500”);
json.put(“message”, “上传文件不存在”);
}
// 开始解压
ZipFile zipFile = null;
try {
// 判断路径是否存在
String temp=zipPath+File.separator+“temp”;//用来存放解压后的文件
File dir = new File(temp);
if (!dir.exists()) {
dir.mkdirs();
}
String targetPath = zipPath + File.separator + file.getName();
File saveFile = new File(targetPath);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
file.transferTo(saveFile);
zipFile = new ZipFile(saveFile); //创建ZipFile
zipFile.setFileNameCharset(“UTF-8”); //防止乱码
zipFile.extractAll(temp);//将zip进行解压
File fileTemp = new File(temp);
// 获取路径下的所有文件
File[] files = fileTemp.listFiles();
for (int i = 0; i < files.length; i++) {
File newFile=files[i];
}
} catch(Exception e) {
json.put(“status”, “500”);
json.put(“message”, “上传失败”);
}
return json;
}

本文地址:https://blog.csdn.net/weixin_44139882/article/details/109842074

相关标签: java