//生成zip
public boolean createCardImgZip(String sourcePath, String zipName) {
// TODO Auto-generated method stub
boolean result = false;
String zipPath = "D:/test/";
File sourceFile = new File(sourcePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (sourceFile.exists() == false) {
System.out.println("File catalog:" + sourcePath + "not exist!");
} else {
try {
if(!new File(zipPath).exists()){
new File(zipPath).mkdirs();
}
File zipFile = new File(zipPath + "/" + zipName + ".zip");
if (zipFile.exists()) {
System.out.println(zipPath + "Catalog File: " + zipName + ".zip" + "pack file.");
} else {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
System.out.println("File Catalog:" + sourcePath + "nothing in there,don't hava to compress!");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
// create .zip and put pictures in
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// read documents and put them in the zip
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
}
result = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
try {
if (null != bis)
bis.close();
if (null != zos)
zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return result;
}
//下载zip
public String downLoadZip() {
String fileName = "test.zip";
String path = "D:\\test\\test.zip";
try {
HttpServletResponse response = ServletActionContext.getResponse();
File file = new File(path);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
response.setContentLength((int) file.length());
response.setContentType("application/zip");// 定义输出类型
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];// 相当于我们的缓存
long k = 0;// 该值用于计算当前实际下载了多少字节
OutputStream myout = response.getOutputStream();// 从response对象中得到输出流,准备下载
// 开始循环下载
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
buff.close();
file.delete();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
java实现生成zip和下载zip
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.template 利用el-table创建一个表格,其中表格的第三列为二维码组件qrcode-vue 基本效果...
- PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP Z...
- 1.另附一:几行代码实现PHP文件打包下载zip 2.另附二:文件打包,下载之使用PHP自带的ZipArchive...
- http://www.365yg.com/group/6446284473024643597/