如何用java读取客户端上传的rar文件

 我来答
w6374638
2017-01-09 · 超过68用户采纳过TA的回答
知道小有建树答主
回答量:276
采纳率:0%
帮助的人:95万
展开全部
直接通过工具类进行解压或者压缩文件即可。

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();
}
}
镭速传输
2024-10-22 广告
大文件传输软件选《镭速企业大文件传输系统》,一站式提升全球企业大文件传输流转效率。不受传统文件传输方式(如FTP、HTTP或CIFS)影响,最大化降低企业大文件传输、大数据传输时延与丢包率,充分利用网络带宽资源,实现各种业务系统与各类操作系... 点击进入详情页
本回答由镭速传输提供
育知同创教育
2018-07-31 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
压缩文件的处理和普通的一样,先上传到服务器,然后通过java的解压缩方式将文件解压出来,再进行读取
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式