I created a zip file (along with the directory) under Windows, as shown below (the code is selected from http://www.exampledepot.com/egs/java.util.zip/CreateZip.html ):
package sandbox; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) {
A newly created zip file can be easily deleted on Windows using http://www.exampledepot.com/egs/java.util.zip/GetZip.html
However, I understand that if I extract the newly created zip file under Linux using the modified version http://www.exampledepot.com/egs/java.util.zip/GetZip.html . The original version does not check the directory with zipEntry.isDirectory ()).
public static boolean extractZipFile(File zipFilePath, boolean overwrite) { InputStream inputStream = null; ZipInputStream zipInputStream = null; boolean status = true; try { inputStream = new FileInputStream(zipFilePath); zipInputStream = new ZipInputStream(inputStream); final byte[] data = new byte[1024]; while (true) { ZipEntry zipEntry = null; FileOutputStream outputStream = null; try { zipEntry = zipInputStream.getNextEntry(); if (zipEntry == null) break; final String destination = Utils.getUserDataDirectory() + zipEntry.getName(); if (overwrite == false) { if (Utils.isFileOrDirectoryExist(destination)) continue; } if (zipEntry.isDirectory()) { Utils.createCompleteDirectoryHierarchyIfDoesNotExist(destination); } else { final File file = new File(destination); // Ensure directory is there before we write the file. Utils.createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile()); int size = zipInputStream.read(data); if (size > 0) { outputStream = new FileOutputStream(destination); do { outputStream.write(data, 0, size); size = zipInputStream.read(data); } while(size >= 0); } } } catch (IOException exp) { log.error(null, exp); status = false; break; } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException exp) { log.error(null, exp); break; } } if (zipInputStream != null) { try { zipInputStream.closeEntry(); } catch (IOException exp) { log.error(null, exp); break; } } } } // while(true) } catch (IOException exp) { log.error(null, exp); status = false; } finally { if (zipInputStream != null) { try { zipInputStream.close(); } catch (IOException ex) { log.error(null, ex); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { log.error(null, ex); } } } return status; }
"MyDirectory \ MyFile.txt" instead of MyFile.txt is placed in the MyDirectory folder.
I am trying to solve the problem by changing the zip file creation code to
String[] filenames = new String[]{"MyDirectory" + "/" + "MyFile.txt"};
But is this an acceptable solution, a hard-coded separator? Will it work under Mac OS? (I do not have a Mac for testing)
java
Cheok yan cheng
source share