下面是解压缩代码 每次运行的时候都再i=1处跳出 也就是FileOutputStream fos=new FileOutputStream (outputpath+"\\"+entryName); 请教了很多人都不能解决 高手请出手相救package test;
import java.io.*;
import java.util.zip.*;
public class unzip{ public int depress(String zipFileName ,String outputpath) {
int i=0;
try {
File file=new File(zipFileName);
FileInputStream fis=new FileInputStream (file);
ZipInputStream in = new ZipInputStream(fis);
ZipEntry entry = null;
i=11;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
i=9;
if (entry.isDirectory()) {
File file = new File(outputpath+"\\"+ entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
}
else
{
i=1;
FileOutputStream fos=new FileOutputStream (outputpath+"\\"+entryName);
i=2;
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
in.close();
fis.close();
//}
}
} catch (IOException e) {
}
System.out.println("解压文件成功");
return i;
}
}
import java.io.*;
import java.util.zip.*;
public class unzip{ public int depress(String zipFileName ,String outputpath) {
int i=0;
try {
File file=new File(zipFileName);
FileInputStream fis=new FileInputStream (file);
ZipInputStream in = new ZipInputStream(fis);
ZipEntry entry = null;
i=11;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
i=9;
if (entry.isDirectory()) {
File file = new File(outputpath+"\\"+ entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
}
else
{
i=1;
FileOutputStream fos=new FileOutputStream (outputpath+"\\"+entryName);
i=2;
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
in.close();
fis.close();
//}
}
} catch (IOException e) {
}
System.out.println("解压文件成功");
return i;
}
}
解决方案 »
- 我是刚学的,麻烦帮忙解释下原因
- SSH框架中 当返回的结果集为Object时jsp页面如何取值?
- eclipse里 TOMCAT5.5 不能启动 错误提示如下 为什么
- 向struts2 的action 传值个别值取不到
- hibernate查询子表对象在页面上找不到!!!!
- 請教關於在JSP中應用Java函數的問題,高手幫幫忙。。。
- Unable to compile class for JSP 高手进来帮帮忙啊!!
- 正则表达式问题,路过免进
- 请问Servlet如何定时执行任务?
- SpringSecurity使用问题,登陆不能抛出自己定义的异常,用户不存在异常,用户尝试登陆次数过多,回到登陆之前访问的页面
- 计算每年的春节日期
- 大家好, 现在学习了java,jsp,struts已经能构建简单的应用平台,但突然有点迷茫,何去何从
in.close();
fis.close();
.中的 in.close(); 是在while循环中的 else中, 即如果不是目录(是文件),则输出到磁盘.然后你就一股脑的把流都关闭了, 如果再有一个不是目录(既 文件)的,又会用这个名为 in的流,而它已被你关闭了, 所以出错. 放到while 循环外面就可以了
}catch (IOException e){
}finally{ //最后再关闭流
try{
if (in != null){
in.close();
}
}catch(IOException ex){}
}
---------------------改完代码如下
----------------
package cn.z.exercise.csdn.unzip;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class Unzip { public static void main(String[] args) throws Exception {
new Unzip().depress("d:/learn.zip", "d:/");
} /* zipFileName 要解压缩的文件 @param outputpath 解压后的文件存放的路径 */
public void depress(String zipFileName, String outputpath) {
// int i = 0;
ZipInputStream in = null;
try {
// 从zipFileName 创建一个 ZipInputStream // 原来是下面这样定义一个file , 没有和下面的冲突吗
// File file = new File(zipFileName);
File zipFile = new File(zipFileName);
FileInputStream fis = new FileInputStream(zipFile);
in = new ZipInputStream(fis); // ZipEntry
ZipEntry entry = null; // i = 11; // entry = in.getNextEntry();
while ((entry = in.getNextEntry()) != null) {
// (entry != null) {
//
FileOutputStream fos = null; String entryName = entry.getName();
// i = 9;
if (entry.isDirectory()) { // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ①②③④⑤⑥⑧⑦⑧⑨⑩
// File file = new File(outputpath + "/" + entryName);
File file = new File(outputpath + entryName);
file.mkdirs();
System.out.println("创建文件夹: " + entryName);
} else {
// i = 1;
// 把fos 放到外面, 效率会好点
fos = new FileOutputStream(outputpath + entryName); // FileOutputStream fos = new FileOutputStream(outputpath +
// "\\"
// + entryName);
// i = 2;
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024]; int len;
while ((len = in.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
// 关闭也放到while 外面, 不然有有时候要出错的
// in.close();
// fis 就不用关了, in.close() 后 . fis也关了
// fis.close();
}
}
System.out.println("解压文件成功 ");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
修完如下
-----------------------------------package cn.z.exercise.csdn.unzip;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class Unzip { /* 测试一下 */
public static void main(String[] args) throws Exception { new Unzip().depress("d:/learn.zip", "d:/");
} /* zipFileName 要解压缩的文件 @param outputpath 解压后的文件存放的路径 */
public void depress(String zipFileName, String outputpath) { /* 保存共解压了多少个文件 */
int fileCount = 0; /* 保存共解压了多少个文件夹 */
int folderCount = 0; // ZipInputStream
ZipInputStream in = null; // ZipEntry 解压文件用到的
ZipEntry entry = null; // File 解压文件用到的
File file = null; try {
// 通过zipFileName 创建一个 ZipInputStream
in = new ZipInputStream(new FileInputStream(zipFileName)); // 保存解压后文件的输出流
FileOutputStream fos = null; while ((entry = in.getNextEntry()) != null) { // 文件名
String entryName = entry.getName(); // 如果是目录
if (entry.isDirectory()) { // 则创建目录
file = new File(outputpath + entryName);
file.mkdirs(); // 目录计数
folderCount++;
} else { // 如果是个文件
// 则解压这个文件
fos = new FileOutputStream(outputpath + entryName);
byte[] buf = new byte[1024];
for (int i = 0; ((i = in.read(buf)) > 0);) {
fos.write(buf, 0, i);
}
// 关闭流
fos.close();
// 文件计数
fileCount++;
}
}
System.out.println("解压完毕. 共解压了" + fileCount + "个文件, 创建了"
+ folderCount + "个文件夹");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
// 关闭流
try {
if (in != null) {
in.close();
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}