linux环境解压时候报错error in opening zip file
工程在本机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());
}
}
}
好几天都没解决,请各位帮帮忙呀!