简体   繁体   中英

Java: Files is not compressed properly(with file permission)

I have making zip file using java. hence, i had face a small problem. Execute files(*.sh and binary files) are compressed is not properly with his file permissions. pls find the following code for make zip

    public static void makeZip(String compressDirPath, String zipName, String outputLoc) 
    throws java.io.IOException
    {
        if(outputLoc.equals("") || outputLoc == null)  outputLoc = ".";
        File compressDir = new File(compressDirPath);
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputLoc+"/"+zipName));
        compress(compressDir, compressDir, zos);
        zos.close();
      }

  private static void compress(File compressDir, File base, ZipOutputStream zos) throws java.io.IOException
  {
    File[] files = compressDir.listFiles();
    byte[] buffer = new byte[18024];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
      if (files[i].isDirectory()) {
        compress(files[i], base, zos);
      } else {
        FileInputStream in = new FileInputStream(files[i]);
        ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
        zos.putNextEntry(entry);
        while (-1 != (read = in.read(buffer))) { zos.write(buffer, 0, read); }
        in.close();
      }
    }
  }

When i have unzip the zipped file, execute files create by normal file. How can i solve this problem?

As far as I get it, file permissions are not stored within zip archives. You can use a tar archive instead. See JTar or org.apache.tools.tar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM