I zip up the contents of the directory, but I get an error when I try to open archived files.
Can anyone tell what is happening with my code? Perhaps I do not allocate enough bytes?
Take a peek inside zipDirectory () and you will see that I zip folders containing special extension files.
I don’t know where the error is, so maybe someone can help me!
Very much appreciated
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) {

source share