(急)jsp上传压缩文件并到服务器端能自动解压

happydaisy1985 2009-08-21 04:43:33
上传压缩文件我可以实现了,就是不知道怎么让压缩文件在指定的目录(即文件上传到的目录)下解压,请大家帮忙!
有代码最好了,呵呵,在线等。。。。
...全文
461 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
star_zk 2012-08-09
  • 打赏
  • 举报
回复
请问楼主 怎么上传zip压缩文件???
kala197 2009-08-24
  • 打赏
  • 举报
回复
学习了
happydaisy1985 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 icewee 的回复:]
Java codeimport java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;publi¡­
[/Quote]
我还有个问题,在jsp里怎么调用这个java类啊?我把编译后的TestZip.class
放在WEB-INF下了,也用import引入了这个文件了,在jsp页面中
<%
TestZip t = new TestZip();
t.zip("D:/zip.zip", "D:/zip");
t.unzip("D:/zip.zip", "D:/newZip");
%>
一运行就出现错误
错误的类文件:......../TestZip.class
类文件包含错误的类:TestZip
请删除该文件或确保该文件位于正确的类路径子目录中。
TestZip t=new TestZip()
帮忙给看看吧,谢谢啦!
  • 打赏
  • 举报
回复
不是服务器自己解压,是调用你写的解压缩方法然后解压

PS:我不知道为什么要在服务端把压缩包解压?
happydaisy1985 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ouyangyh 的回复:]
现在服务器端都可以自解压的
打包成war格式就可以了
其实就是jar格式的
[/Quote]
现在服务器端都可以自解压的?
能说明白一点吗?
ouyangyh 2009-08-24
  • 打赏
  • 举报
回复
现在服务器端都可以自解压的
打包成war格式就可以了
其实就是jar格式的
zwei27 2009-08-22
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipToFile {


public static boolean zipToFile(String sZipPathFile, String sDestPath) {
boolean flag = false;
try {

FileInputStream fins = new FileInputStream(sZipPathFile);

ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + "//" + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
String zfilePath = zfile.getAbsolutePath();
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();

if (zfilePath.endsWith(".zip")) {
zipToFile(zfilePath, zfilePath.substring(0, zfilePath
.lastIndexOf(".zip")));
}

}
}
fins.close();
zins.close();
// if necessary, delete original zip-file
File file = new File(sZipPathFile);
file.delete();
flag = true;
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
}
return flag;

}
public static void main(String[] args) {
String sZipPathFile = "E:\\software\\apache-tomcat-6.0.16\\webapps\\Wochz\\01\\English\\0\\a.zip";
String sDestPath = "E:\\software\\apache-tomcat-6.0.16\\webapps\\Wochz\\01\\English\\0\\";
System.out.println(ZipToFile.zipToFile(sZipPathFile, sDestPath));
}


}
這個能實現上傳並自動解壓,然後刪除那個文件隻留下壓縮文件,有一點那個文件名必須是字母形式
lvsh870228 2009-08-22
  • 打赏
  • 举报
回复
顶!
javagxc 2009-08-22
  • 打赏
  • 举报
回复
上传你会了,在上传后边加个解压就OK了.

Java实现压缩与解压缩功能源码
长公子冰 2009-08-21
  • 打赏
  • 举报
回复

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class TestZip {

public void zip(String zipFileName, String inputFile) throws Exception {
zip(zipFileName, new File(inputFile));
}

public void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("zip done");
out.close();
}

public void unzip(String zipFileName, String outputDirectory)
throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ((z = in.getNextEntry()) != null) {
System.out.println("unziping " + z.getName());
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator
+ name);
} else {
File f = new File(outputDirectory + File.separator
+ z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
}

public void zip(ZipOutputStream out, File f, String base) throws Exception {
System.out.println("Zipping " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}

}

public static void main(String[] args) {
try {
TestZip t = new TestZip();
t.zip("D:/zip.zip", "D:/zip");
t.unzip("D:/zip.zip", "D:/newZip");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}

}

【如鱼饮水】 2009-08-21
  • 打赏
  • 举报
回复
java.unil.zip.ZipFile
解压
老紫竹 2009-08-21
  • 打赏
  • 举报
回复
1 你会解压缩吗?
2 既然会,那么把上传的文件保存成文件,然后解开不就行了!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧