// 获取根目录
String rootURL = request.getSession().getServletContext()
.getRealPath("/");
// 获取zip包
String filerealpath = "URL";
// 获取压缩文件中的文件的输入流
ZipFile zf = null;
ZipInputStream zin = null;
ZipEntry ze = null;
InputStream is = null;
OutputStream fos = null;
try {
zf = new ZipFile(filerealpath);
zin = new ZipInputStream(new BufferedInputStream(
new FileInputStream(filerealpath)));
ze = zin.getNextEntry();
is = zf.getInputStream(ze);
// 将文件输出到指定地址
File file1 = new File(rootURL + "/download/" + fileName + hz);
// URLEncoder.encode(filename,"utf-8");
fos = new FileOutputStream(file1);
log.writeLog("文件名:" + fos);
byte[] buf = new byte[4096]; // 创建字节数组,作为临时缓冲
int len = 0;
while ((len = is.read(buf, 0, buf.length)) != -1) { // 循环从文件输入流中读取数据
fos.write(buf, 0, len); // 写入到文件输出流中
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
is = null;
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
fos = null;
}
}
}
将压缩文件解压到指定目录
最新推荐文章于 2024-08-15 11:32:13 发布