今天遇到个bug,在导出数据打成zip包的时候,在本机(xp)上成功,在服务器(linux)上报异常,
异常如下:ZIP file must have at least one entry。
上网查了,但是更多的是关于路径名有中文的问题,但是我在本机和服务器上都是英文目录,以下是我
的部分代码:
/**
* Creates a Zip archive. If the name of the file passed in is a directory,
* the directory's contents will be made into a Zip file.
*/
public static String makeZip(String fileName) throws IOException,
FileNotFoundException {
File file = new File(fileName);
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
// Call recursion.
recurseFiles(file, zos);
} finally {
if (null != zos) {
zos.close();
}
}
return file + ".zip";
}
private static void recurseFiles(File file, ZipOutputStream zos)
throws IOException, FileNotFoundException {
if (file.isDirectory()) {
// Create an array with all of the files and subdirectories
// of the current directory.
String[] fileNames = file.list();
if (fileNames != null) {
// Recursively add each array entry to make sure that we get
// subdirectories as well as normal files in the directory.
for (int i = 0; i < fileNames.length; i++) {
recurseFiles(new File(file, fileNames[i]), zos);// 给子目录里的文件打包!!
}
}
}
// Otherwise, a file so add it as an entry to the Zip file.
else {
byte[] buf = new byte[1024];
int len;
// Create a new Zip entry with the file's name.
ZipEntry zipEntry = new ZipEntry(file.getName());
// Create a buffered input stream out of the file
// we're trying to add into the Zip archive.
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);
// Read bytes from the file and write into the Zip archive.
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
// Close the input stream.
in.close();
// Close this entry in the Zip stream.
zos.closeEntry();
}
}
相关推荐
23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java自定义异常.zip23.java...
java大作业:简单的FlappyBird游戏.zipjava大作业:简单的FlappyBird游戏.zip java大作业:简单的FlappyBird游戏.zipjava大作业:简单的FlappyBird游戏.zip java大作业:简单的FlappyBird游戏.zipjava大作业:简单的...
java 打包 目录 文件 zip java 打包 目录 文件 zip java 打包 目录 文件 zip
java课程设计:基于springBoot的家教管理系统.zip java课程设计:基于springBoot的家教管理系统.zip java课程设计:基于springBoot的家教管理系统.zip java课程设计:基于springBoot的家教管理系统.zip java课程设计...
java课程设计:基于Java Swing 编写的水果贪吃蛇小游戏.zip java课程设计:基于Java Swing 编写的水果贪吃蛇小游戏.zip java课程设计:基于Java Swing 编写的水果贪吃蛇小游戏.zip java课程设计:基于Java Swing ...
20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多异常捕获.zip20.java多...
Java安卓:猜地鼠游戏.zipJava安卓:猜地鼠游戏.zipJava安卓:猜地鼠游戏.zip Java安卓:猜地鼠游戏.zipJava安卓:猜地鼠游戏.zipJava安卓:猜地鼠游戏.zip Java安卓:猜地鼠游戏.zipJava安卓:猜地鼠游戏.zipJava...
java: 读取D:\Develop\maven\repository\org\aspectj\aspectjweaver\1.9.6\aspectjweaver-1.9.6.jar时出错; error in opening zip file 提示jar出问题,找到此处,删除重新下载,但是jar依旧是损坏状态
应用java来压缩需要打包的文件,在系统管理中将毛哥路径或者某个文件夹压缩成zip包
主要给大家介绍了关于tomcat启动报:java.util.zip.ZipException错误的解决方法,文中通过示例代码介绍的非常详细,同样遇到这个问题的朋友可以参考借鉴,下面随着小编来一起学习学习吧
12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常的概念.zip12.java异常...
14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理机制.zip14.java异常处理...
java打包文件夹为zip 或者打包文件 ,通过测试 zipcomcompressor 单个文件打包,循环打包速度会快非常多,要是打包文件夹,速度很慢。
JAVAWEB项目,实现将服务器文件打包成.zip文件,然后再下载到本地上。
java打包压缩ZIP 里面详细讲解了如何将一个文件打包 如何解压一个包
因项目需求,需要在线生成world文档,并且需要批量生成world,那么就可以用zip打包所有world进行下载,这里提供的是一个简单的demo,如需了解在线生成文件,并打包到zip,可以问我
Java根据Url把多个文件下载到指定的文件夹目录,然后再将文件夹目录打包成zip导出,包括子目录也可以打包,有个简单的导出html页面,点击导出按钮下载zip。
13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip13.java异常的体系结构.zip...