解压Zip
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile; /**
* 可以处理中文文件名
*/
public class UnZip2
{
private static final int buffer = 2048; public static void main(String[] args)
{
unZip("D:\\ss\\test.zip");
} public static void unZip(String path)
{
int count = -1;
int index = -1;
String savepath = "";
boolean flag = false; File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null; savepath = path.substring(0, path.lastIndexOf("\\")) + "\\"; try
{
ZipFile zipFile = new ZipFile(path); Enumeration<?> entries = zipFile.getEntries(); while(entries.hasMoreElements())
{
byte buf[] = new byte[buffer]; ZipEntry entry = (ZipEntry)entries.nextElement(); String filename = entry.getName();
index = filename.lastIndexOf("/");
if(index > -1)
filename = filename.substring(index+1); filename = savepath + filename; flag = isPics(filename);
if(!flag)
continue; file = new File(filename);
file.createNewFile(); is = zipFile.getInputStream(entry); fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer); while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count );
} fos.close(); is.close();
} zipFile.close(); }catch(IOException ioe){
ioe.printStackTrace();
}
} public static boolean isPics(String filename)
{
boolean flag = false; if(filename.endsWith(".jpg") || filename.endsWith(".gif") || filename.endsWith(".bmp") || filename.endsWith(".png"))
flag = true; return flag;
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; public class ImageFileService {
private static final int buffer = 2048; public static void main(String[] args) {
unZip("D:\\YXDoc\\20131212001.zip");
} public static void unZip(String path) {
int count = -1;
int index = -1; String savepath = "";
boolean flag = false;
// 解压路径
savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";
try {
BufferedOutputStream bos = null;
ZipEntry entry = null;
FileInputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(fis)); while ((entry = zis.getNextEntry()) != null) {
byte data[] = new byte[buffer];
String temp = entry.getName();
flag = isPics(temp);
if (!flag)
continue;
index = temp.lastIndexOf("/");
if (index > -1)
temp = temp.substring(index + 1);
temp = savepath + temp;
File f = new File(temp);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos, buffer);
while ((count = zis.read(data, 0, buffer)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
bos.close();
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static boolean isPics(String filename) {
boolean flag = false;
if (filename.endsWith(".jpg") || filename.endsWith(".gif")
|| filename.endsWith(".bmp") || filename.endsWith(".png"))
flag = true;
return flag;
}
}
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String localPath=(String)request.getAttribute("localPath");
String fileName=(String)request.getAttribute("fileName");
localPath="D://";
fileName="12323.pdf"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<% out.clear();
out = pageContext.pushBody();
response.setContentType("application/pdf");
try {
String sourcePDFPath =localPath+fileName;
//判断该路径下的文件是否存在
File file = new File(sourcePDFPath);
if (file.exists()){
DataOutputStream ou = new DataOutputStream(response.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(sourcePDFPath));
byte[] b = new byte[2048];
while ((in.read(b)) != -1){
ou.write(b);
ou.flush();
}
in.close();
ou.close();
}else{
out.print(sourcePDFPath + " 文件不存在!");
}
}catch (Exception e) {
out.println(e.getMessage());
}%>
<body>
</body>
</html>
解压Zip的更多相关文章
- 【VC++技术杂谈008】使用zlib解压zip压缩文件
最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...
- 通过javascript在网页端解压zip文件并查看压缩包内容
WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说: 如果前端的代 ...
- Ubuntu 14 如何解压 .zip、.rar 文件?
.zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压? [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...
- linux下压缩与解压(zip、unzip、tar)详解
linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...
- 解决ubuntu解压zip文件名乱码的问题
1. 安装7-zip 和 convmv : 命令: sudo apt-get install convmv p7zip-full 2. 解压zip文件: 命令:LANG=C 7z e yourZIPf ...
- linux解压zip、bz、bz2、z、gz、tar(解包)
zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...
- C# 解压zip压缩文件
此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...
- PHP 的解压缩ZipArchive中的extractTo()方法 LINUX+nginx环境中解压zip时文件丢失的问题
在项目中要用ZipArchive解压ZIP文件,起初測试环境在WINDOWS平台中,測试通过,换到 LINUX+nginx 的环境中时 就出问题了(ZIP包中有文件和目录一共3百多个文件,大部分是带汉 ...
- java 解压 zip 包并删除
需求是这样的, 在服务器上有 运营上传的zip 包,内容是用户的照片,我需要做的是 获取这些照片上传,并保存到 数据库. 这里面的 上传照片,保存数据库都不难,主要问题是解压zip包,和删除zip ...
- Java 解压zip压缩包
因为最近项目需要批量上传文件,而这里的批量就是将文件压缩在了一个zip包里,然后读取文件进行解析文件里的内容. 因此需要先对上传的zip包进行解压.以下直接提供代码供参考: 1.第一个方法是用于解压z ...
随机推荐
- POJ——3984迷宫问题(BFS+回溯)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14568 Accepted: 8711 Description ...
- 常州模拟赛d3t2 灰狼呼唤着同胞
题目背景 我的母亲柯蒂丽亚,是一个舞者.身披罗纱,一身异国装扮的她,来自灰狼的村子. 曾经在灰狼村子担任女侍的她,被认定在某晚犯下可怕的罪行之后,被赶出了村子. 一切的元凶,都要回到母亲犯下重罪的那一 ...
- P1582 倒水 (二进制)
题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...
- OpenJ_Bailian3375
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- SQLite数据库相关操作
一.创建数据库 这里创建了note便签数据表,字段有noteId.noteTitle.noteTime.noteInfo ); // TODO Auto-generated constructor ...
- Resin Thread Dump
[2015/08/25 20:50:13.254] {ThreadLauncher2[ThreadPool[system]]-1} Thread Dump generated Tue Aug 25 2 ...
- Laravel 之Artisan
简介: Artisan是Laravel中自带的命令行工具的名称: 由强大的Symfony Console组件驱动的: 提供了一些对应用开发有帮助的命令: 查看所有可用的Artisan的命令 php a ...
- Heavy Transportation(最短路)
poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...
- mongDB的常用操作总结
目录 常用查询: 查询一条数据 查询子元素集合:image.id gte: 大于等于,lte小于等于... 查询字段不存在的数据not 查询数量: 常用更新 更新第一条数据的一个字段: 更新一条数据的 ...
- Ubuntu 16.04安装Mac OS 12虚拟机资源(没成功,但资源还是可以用)
整理的Mac OS 12虚拟机资源.装虚拟机基本是按这样的套路: 1.先装VM 2.破解VM使其支持Mac OS 12,这个脚本基本是全平台支持,可以看里面的教程文档. 3.用镜像安装系统. 资源: ...