java使用 zipoutputstream 进行解压缩时提示:不可预料的压缩文件末端

private static void zip(File filein, String basepath, ZipOutputStream out) throws IOException {
		FileInputStream in = null;
		try {
			if (filein.isDirectory()) {
				File files[] = filein.listFiles();
				/*if (basepath != "") {
					out.putNextEntry(new ZipEntry(basepath + File.separator));
				}
				basepath = basepath.length() == 0 ? "" : basepath +File.separator;*/
				for (int i = 0; i < files.length; i++) {
                  zip(files[i], basepath+File.separator+files[i].getName(), out);  
				}
			} else {
				out.putNextEntry(new ZipEntry(basepath));
				in= new FileInputStream(filein);
				int a = 0;
				while ((a = in.read()) != -1) {
					out.write(a);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (in != null) {
				in.close();
			   }
			
		} 

 我出现这个错误的原因是没有执行  zipput.close()将输出流关闭,导致 压缩出来的文件有问题 

File file = new File("D:\\test");
		ZipOutputStream zipout = new ZipOutputStream(
				new FileOutputStream(new File("D:" + File.separator + "test.zip")));
		zip(file, file.getName(), zipout);
		zipout.close();

猜你喜欢

转载自blog.csdn.net/liu6219364/article/details/82785922