java如何解压页面上传到服务器的zip文件

我是通过Form中FormFile类型的对象取得上传的zip文件,如何将zip文件中的子文件解压到服务器的某一路径下?publicvoidimportShuoMingSh... 我是通过Form中FormFile类型的对象取得上传的zip文件,如何将zip文件中的子文件解压到服务器的某一路径下?
public void importShuoMingShu(FormFile file) throws BaseException{
// 读取文件流,转成excel对象,分析对象,存入数据
try {
InputStream stream = file.getInputStream();
BufferedInputStream origin = new BufferedInputStream(stream, 2048);
ZipInputStream zout = new ZipInputStream(origin);
ZipEntry zipEntry = null;
//循环遍历zip中的每一个文件进行处理
while( ( zipEntry = zout.getNextEntry() ) != null ){
//如果是文件夹,不做处理
if(zipEntry.isDirectory()){
continue;
}
//取得文件名
String xlsName = zipEntry.getName().trim();
//取得当前该文件流
File xlsFile = new File("c://" + xlsName);
OutputStream outputS = new FileOutputStream(xlsFile);
ZipFile zf = (ZipFile)file;
InputStream inputS = zf.getInputStream(zipEntry);
byte[] by = new byte[100000];
int c;
while ((c = inputS.read(by)) != -1) {
outputS.write(by, 0, c);
}
outputS.flush();
outputS.close();
}
以上是我的代码 不过在ZipFile zf = (ZipFile)file;处出现了强制转型异常,请高手帮忙看看该如何解决这个问题。谢谢
展开
 我来答
198901245631
推荐于2017-10-08 · TA获得超过3.5万个赞
知道大有可为答主
回答量:9037
采纳率:92%
帮助的人:1662万
展开全部
直接通过工具类进行解压或者压缩文件即可。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration<? extends ZipEntry> entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}

/**
* 将输入流中的数据写到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) > 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}

/**
* 安全关闭多个流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}

/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
百度网友dc81944bb
2009-11-21 · TA获得超过572个赞
知道小有建树答主
回答量:793
采纳率:0%
帮助的人:715万
展开全部
你这里写错了。 这是以前我写的解zip和压缩zip的
private 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();
}

private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.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 org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友aa4b4ac
推荐于2017-10-04 · TA获得超过189个赞
知道小有建树答主
回答量:127
采纳率:0%
帮助的人:129万
展开全部
这个转换肯定是会出错的,struts 的formFile跟zipFile没有直接关系,怎么能这么强制转化呢?
建议
1. 把文件保存到一个临时目录(保存为zip文件)
2. 读取这个文件
3. 抽取想要的文件
4. 把临时文件删除
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式