linux环境解压时候报错error in opening zip file
上传到服务器linux环境下,压缩可以,解压就不行了
本机和服务器JDK是一样版本的,都是用的tomcat
报错是error in opening zip file
压缩代码
package com.inspur.common.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import de.schlichtherle.io.*;
import de.schlichtherle.util.zip.ZipEntry;
import de.schlichtherle.util.zip.ZipOutputStream;
import java.text.*;
import java.util.*;
public class ZipCompress {
/**
* 递归压缩文件
*
* @param source
* 源路径,可以是文件,也可以目录
* @param destinct
* 目标路径,压缩文件名
* @throws IOException
* @throws ParseException
*/
public static void compress(String timeTxt, String source, String destinct)
throws IOException, ParseException {
try{
// String timeTxt = "/home/tomcat/webapps/ROOT/upload/markTime.txt";
TxtRead txtRead = new TxtRead();
String sLastTime = txtRead.doReadFromByteToChar(timeTxt); // 获得上次压缩时间
List fileList = loadFilename(new File(source), sLastTime);
File zipfile = new File(destinct);
FileOutputStream fout = new FileOutputStream(zipfile);
ZipOutputStream zout = new ZipOutputStream(fout, "GBK");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
new File(destinct)));
byte[] buffere = new byte[8192];
int length;
BufferedInputStream bis;
for (int i = 0; i < fileList.size(); i++) {
File file = (File) fileList.get(i);
zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
bis = new BufferedInputStream(new FileInputStream(file));
while (true) {
length = bis.read(buffere);
if (length == -1)
break;
zos.write(buffere, 0, length);
}
bis.close();
zos.closeEntry();
}
zos.close();
// 保存本次操作时间
DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
String time = dateFormat.format(new Date());
txtRead.doWriteAddBuffer(timeTxt, time);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
* 递归获得该文件下所有文件名(不包括目录名)
*
* @param file
* @return
* @throws ParseException
*/
private static List loadFilename(File file, String sLastTime)
throws ParseException {
List filenameList = new ArrayList();
Long lastTime = Long.parseLong(sLastTime);
if (file.isFile()) {
Long fileTime = file.lastModified();
SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");
String tempFileTime = formatter.format(new Date(fileTime));
Long lFileTime = Long.parseLong(tempFileTime);
if (lFileTime > lastTime) { // 文件的修改时间迟于参数时间
filenameList.add(file);
}
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
filenameList.addAll(loadFilename(f, sLastTime));
}
}
return filenameList;
}
/**
* 获得zip entry 字符串
*
* @param base
* @param file
* @return
*/
private static String getEntryName(String base, File file) {
File baseFile = new File(base);
String filename = file.getPath();
if (baseFile.getParentFile().getParentFile() == null)
return filename.substring(baseFile.getParent().length());
return filename.substring(baseFile.getParent().length() + 1);
}
}
解压代码
package com.inspur.common.util;
import java.io.BufferedOutputStream;
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.ZipException;
import java.util.zip.ZipFile;
public class ZipDecompression {
/** */
/**
* @param args
* @throws IOException
*/
/*
* public static void main(String[] args) throws IOException {
* decompression("E:\\new\\newtext.rar", "E:\\new"); }
*/
/** */
/**
* 解压ZIP文件
*
* @param zipFile
* 要解压的ZIP文件路径
* @param destination
* 解压到哪里
* @throws IOException
*/
public static void decompression(String zipFile, String destination)
throws IOException {
try{
ZipFile zip = new ZipFile(zipFile);
Enumeration en = zip.entries();
ZipEntry entry = null;
byte[] buffer = new byte[8192];
int length = -1;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;
while (en.hasMoreElements()) {
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
continue;
}
input = zip.getInputStream(entry);
file = new File(destination, entry.getName());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
length = input.read(buffer);
if (length == -1)
break;
bos.write(buffer, 0, length);
}
bos.close();
input.close();
}
zip.close();
}catch(ZipException e){
System.out.println(zipFile+"-错误:"+e.getMessage());
}
}
}
好几天都没解决,请各位帮帮忙呀!
相关推荐
c++ 解压zip文件到指定文件夹
c++ 解压zip文件到指定文件夹,c++ 解压zip文件到指定文件夹
从ADS8681.zip到zip全攻略:解压、密码、报错与开发实战
ZIP压缩包是数字世界最常见的文件容器,从芯片资料、软件安装包到固件升级,几乎无处不在。然而,一个看似简单的zip文件,背后却藏着编码混乱、密码保护、结构损坏(如EOCD缺失)等一系列问题。理解zip的原理,掌握正确的工具与排查方法,能极大提升工作效率。对于工业级ADC芯片ADS8681这类硬件开发场景,配套资料通常以zip形式分发,包含数据手册、驱动源码和参考设计。通过命令行如unzip、Python的zipfile模块,或专业工具如7-Zip与Bandizip,我们既能高效解压、修复损坏包,也能处理密码
MathModelAgent项目zip包解压与部署实战:从EOCD报错到conda环境配置
在项目交付与代码分发中,ZIP压缩包是最常见的载体,但解压过程常常遇到“file is not a zip file”或“invalid zip archive: could not find EOCD”等报错,根源在于文件截断、扩展名错误或分卷缺失。面对MathModelAgent这类包含多模块和依赖的数学建模智能体项目,解压方式不当会直接导致后续环境配置失败。从检查文件完整性、使用7-Zip或unzip规范解压,到通过conda安装requirements.txt依赖,再到配置JRE17等特殊运行环境,
zip文件解压报错全解析:从EOCD定位到修复与安全识别
在文件传输与资源管理中,zip格式是最常见的压缩载体,但“file is not a zip file”或“could not find EOCD”等报错却频繁困扰开发者与运维人员。理解zip文件头、中央目录与EOCD的结构原理,是定位解压失败的关键。通过file命令、哈希校验和unzip -t等手段,可在解压前快速验证文件完整性。对于损坏的压缩包,zip -FF与7-Zip能有效修复部分截断或结构损坏。同时,密码处理、分卷合并、Linux命令行实战以及跨工具选型,均是日常高频技术场景。本文以DUL5108
问题解决:在Ubuntu16.04.5上解压zip文件失败 Error: Can not open file as archive
问题如下: 通过百度查找解决方案,下载并安装了p7zip_16.02_src_all.tar.bz2 (参考地址:https://blog.csdn.net/mr_muli/article/details/89524690),再次尝试解压失败 解决如下: 在网上百度后处理依旧报错,最后是因为在Ubuntu上下载vulhub-master.zip时,由于网络各种有中断,导致文件不完整。为了保险起见,我在自己物理机上重新下载,再将下载好拖进虚拟机里解压即可。 之前下的不完整文件记得及时删除。 …… 至此,解压成功 作者:7Riven


Eclipse
