Java util zip 创建“损坏”的 zip 文件
我正在压缩目录的内容,但在尝试打开压缩的文件时遇到错误。
谁能说出我的代码是怎么回事?也许我没有分配足够的字节?
查看zipDirectory()内部,您将看到我正在压缩包含特殊扩展文件的文件夹。
不确定错误发生在哪里,所以也许有人可以帮助我!
非常感谢
    private void zipDirectory() {
       File lazyDirectory = new File(defaultSaveLocation);
       File[] files = lazyDirectory.listFiles();
       for (File file : files) {
          if (file.isDirectory()) {
            System.out.println("Zipping up " + file);
            zipContents(file);
            }
        }       
    }
public static void addToZip(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
    System.out.println("Writing '" + fileName + "' to zip file");
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }
    zos.closeEntry();
    fis.close();
    }
public static void zipContents(File dirToZip) {
    List<File> fileList = new ArrayList<File>();
    File[] filesToZip = dirToZip.listFiles();
    for (File zipThis : filesToZip) {
        String ext = "";
        int i = zipThis.toString().lastIndexOf('.');
        if (i > 0) {
            ext = zipThis.toString().substring(i+1);
        }
        if(ext.matches("cpp|bem|gz|h|hpp|pl|pln|ppcout|vec|xml|csv")){
            fileList.add(zipThis);
        }
    }
    try {
        FileOutputStream fos = new FileOutputStream(dirToZip.getName() + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        for (File file : fileList) {
            addToZip(file.toString(), zos);
        }
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
    }

 
					 
				 
				    		 
				    		 
				    		 
				    		