linux环境解压时候报错error in opening zip file

baby414 2011-09-19 12:03:18
工程在本机windows环境下运行压缩解压一切OK
上传到服务器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());
}
}

}


好几天都没解决,请各位帮帮忙呀!

...全文
1746 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fainfy 2011-09-19
  • 打赏
  • 举报
回复
弱弱的问一句:楼主在linux上面是不是想要解压rar文件?
如果是rar文件不能解压,这很正常。因为linux不支持rar压缩,楼主应该用zip。
如果楼主是zip压缩文件不能解压请把详细错误信息提交上来。
baby414 2011-09-19
  • 打赏
  • 举报
回复
找到原因了,是内外网的文件传输出问题了,压缩文件直接复制出去程序不报错的
softroad 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fainfy 的回复:]

弱弱的问一句:楼主在linux上面是不是想要解压rar文件?
如果是rar文件不能解压,这很正常。因为linux不支持rar压缩,楼主应该用zip。
如果楼主是zip压缩文件不能解压请把详细错误信息提交上来。
[/Quote]

学习了


public static File getFileFromZipByName(File zip, String path, String findName) {
File file = null;

try {
ZipFile zipFile = new ZipFile(zip);
Enumeration enu = zipFile.getEntries();

while(enu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enu.nextElement();
if(findName.equals(entry.getName())) {
file = new File(path + findName);
InputStream in = zipFile.getInputStream(entry);
FileOutputStream out = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[256];

while((len = in.read(bytes, 0, bytes.length)) != -1) {
out.write(bytes, 0, len);
}

out.flush();
out.close();
in.close();
}
}
}
catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}

return file;
}
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而高性能,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。  本课程的YOLOv5使用ultralytics/yolov5,在Windows和Ubuntu系统上分别做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集(自动划分训练集和验证集)、修改配置文件、使用wandb训练可视化工具、训练自己的数据集、测试训练出的网络模型和性能统计。 除本课程《YOLOv5实战训练自己的数据集(Windows和Ubuntu演示)》外,本人推出了有关YOLOv5目标检测的系列课程。请持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209 《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284  

58,454

社区成员

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

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