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的更多相关文章

  1. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  2. 通过javascript在网页端解压zip文件并查看压缩包内容

    WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说:     如果前端的代 ...

  3. Ubuntu 14 如何解压 .zip、.rar 文件?

    .zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压? [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...

  4. linux下压缩与解压(zip、unzip、tar)详解

    linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...

  5. 解决ubuntu解压zip文件名乱码的问题

    1. 安装7-zip 和 convmv : 命令: sudo apt-get install convmv p7zip-full 2. 解压zip文件: 命令:LANG=C 7z e yourZIPf ...

  6. linux解压zip、bz、bz2、z、gz、tar(解包)

    zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...

  7. C# 解压zip压缩文件

    此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...

  8. PHP 的解压缩ZipArchive中的extractTo()方法 LINUX+nginx环境中解压zip时文件丢失的问题

    在项目中要用ZipArchive解压ZIP文件,起初測试环境在WINDOWS平台中,測试通过,换到 LINUX+nginx 的环境中时 就出问题了(ZIP包中有文件和目录一共3百多个文件,大部分是带汉 ...

  9. java 解压 zip 包并删除

    需求是这样的,  在服务器上有 运营上传的zip 包,内容是用户的照片,我需要做的是 获取这些照片上传,并保存到 数据库. 这里面的 上传照片,保存数据库都不难,主要问题是解压zip包,和删除zip ...

  10. Java 解压zip压缩包

    因为最近项目需要批量上传文件,而这里的批量就是将文件压缩在了一个zip包里,然后读取文件进行解析文件里的内容. 因此需要先对上传的zip包进行解压.以下直接提供代码供参考: 1.第一个方法是用于解压z ...

随机推荐

  1. Leetcode 334.递增的三元子序列

    递增的三元子序列 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n- ...

  2. ubuntu上传项目到github

    https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70544159

  3. 九度oj 题目1472:求两个多项式的和

    题目描述: 输入两个多项式,计算它们的和. 每个多项式有若干对整数表示,每组整数中,第一个整数表示系数(非0),第二个整数表示该项的次数. 如由3 3 5 -2 1 4 0表示3x^5 - 2 * x ...

  4. You need to install the perl-doc package to use this program

    You need to install the perl-doc package to use this program 解决方案:apt-get install perl-doc

  5. writeValueAsString封装成工具类

    封装成工具类 <span style="font-family:Microsoft YaHei;">public static String toJsonByObjec ...

  6. farm

    farm 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 White Rabbit has ...

  7. R语言入门视频笔记--10--数据挖掘

    这里来挖掘超市购物车数据. 名词: 1.挖掘数据集:购物篮数据 2.挖掘目标:关联规则 3.关联规则:牛奶=>鸡蛋[支持度=2%,置信度=60%] 4.指出度:分析中的全部事务的2%同时购买了牛 ...

  8. Executors

    提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...

  9. T3139 栈练习3 codevs

    http://codevs.cn/problem/3139/ 题目描述 Description 比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这 ...

  10. stm32f103定时器

    1)TIM_TimeBaseInitTypeDef 时基初始化结构体,它包括了四个成员函数:TIM_ClockDivision.TIM_CounterMode.TIM_Period.TIM_Presc ...