Adding debug zip archive

Therefore, I was interested in adding files to the zip archive, and I came across several users who asked this question before, and another user gave this piece of code as a solution to this problem:

public static void updateZip(File source, File[] files, String path){ try{ File tmpZip = File.createTempFile(source.getName(), null); tmpZip.delete(); if(!source.renameTo(tmpZip)){ throw new Exception("Could not make temp file (" + source.getName() + ")"); } byte[] buffer = new byte[4096]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source)); for(int i = 0; i < files.length; i++){ System.out.println(files[i].getName()+"being read"); InputStream in = new FileInputStream(files[i]); out.putNextEntry(new ZipEntry(path + files[i].getName())); for(int read = in.read(buffer); read > -1; read = in.read(buffer)){ out.write(buffer, 0, read); } out.closeEntry(); in.close(); } for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){ if(!zipEntryMatch(ze.getName(), files, path)){ out.putNextEntry(ze); for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){ out.write(buffer, 0, read); } out.closeEntry(); } } out.close(); tmpZip.delete(); }catch(Exception e){ e.printStackTrace(); } } private static boolean zipEntryMatch(String zeName, File[] files, String path){ for(int i = 0; i < files.length; i++){ if((path + files[i].getName()).equals(zeName)){ return true; } } return false; } 

I created a mini-program to test this method, and this is the method that does all the work:

  private static void appendArchive() { String filename = "foo"; File[] filelist = new File[10]; int i = 0; String temp = ""; while (!filename.trim().equals("")) { System.out .println("Enter file names to add, then enter an empty line"); filename = getInput(); filelist[i] = new File(filename, filename); System.out.println("Adding " + filelist[i].getName()); } System.out .println("What is the name of the zip archive you want to append"); File zipSource = new File(getInput() + ".zip", "testZip.zip"); try { Archiver.updateZip(zipSource, filelist, ""); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

Whenever I try to run this program, I get this error, and then the following:

 java.lang.Exception: Could not make temp file (testZip.zip) at Archiver.updateZip(Archiver.java:68) at main.appendArchive(main.java:62) at main.main(main.java:29) 

I suspected that the mail file that I skipped was considered open for some reason, and therefore the renaming method did not work on windows, so I tried instead to use the constructor for the zip file that you see now. What exactly am I doing wrong here. My test input is 2 for the file and 2 (which is added to 2.zip). This should not be a directory problem, as the files are generated by the program. the files

0
source share
1 answer

Jobs are found for me. I suspect you might check the operation of tmpZip.delete() .

 if (!tmpZip.exists() || tmpZip.delete()) { // ... Continue } else { // ... File is locked } 

UPDATE

I played with the code and there are some additional disadvantages ...

When adding old records to a new file, you use the existing ZipEntry record. This will fail if the resulting compression is different. You should create a new use of ZipEntry add, which instead

 ZipEntry ne = new ZipEntry(ze.getName()); out.putNextEntry(ne); // Write bytes to file... out.closeEntry(); 

You never close ZipInputStream , which means that tmpZip.delete() at the end will fail.

Error handling is next to nonexistent ...

 ZipInputStream zin = null; ZipOutputStream out = null; try { // Append zip ... } finally { try { zin.close(); } catch (Exception exp) { } try { out.close(); } catch (Exception exp) { } } 

Will prevent future file locks (I did not consciously catch an IOException , since I personally believe that it should be returned to the called)

You should not overwrite the existing zip file that you have finished. You must create a temporary file for the new zip file, write all files to it, add existing files, and after execution replace the existing file with a temporary one.

This means that if something goes wrong, you have not destroyed the existing zip file.

IMHO

0
source

All Articles