2010-10-18 43 views
7
public static void main(String argv[]) { 
    try { 
     String date = new java.text.SimpleDateFormat("MM-dd-yyyy") 
       .format(new java.util.Date()); 
     File inFolder = new File("Output/" + date + "_4D"); 
     File outFolder = new File("Output/" + date + "_4D" + ".zip"); 
     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
       new FileOutputStream(outFolder))); 
     BufferedInputStream in = null; 
     byte[] data = new byte[1000]; 
     String files[] = inFolder.list(); 
     for (int i = 0; i < files.length; i++) { 
      in = new BufferedInputStream(new FileInputStream(
        inFolder.getPath() + "/" + files[i]), 1000); 
      out.putNextEntry(new ZipEntry(files[i])); 
      int count; 
      while ((count = in.read(data, 0, 1000)) != -1) { 
       out.write(data, 0, count); 
      } 
      out.closeEntry(); 
     } 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

Tôi đang cố nén một thư mục chứa thư mục con. Đang cố gắng nén thư mục có tên 10-18-2010_4D. Chương trình trên kết thúc bằng ngoại lệ sau. Vui lòng tư vấn về cách xóa vấn đề.Nén thư mục chứa thư mục con

java.io.FileNotFoundException: Output\10-18-2010_4D\4D (Access is denied) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at ZipFile.main(ZipFile.java:17) 
+0

Tên thư mục trong trường hợp ngoại lệ và tên thư mục bạn đã đề cập khác nhau. – ivorykoder

+0

có thể trùng lặp với [thư mục trong tệp zip khi sử dụng java.util.zip.ZipOutputStream] (http://stackoverflow.com/questions/740375/directories-in-a-zip-file-when-using-java-util -zip-zipoutputstream) –

Trả lời

5

Bạn cần phải kiểm tra xem các tập tin là một thư mục vì bạn không thể vượt qua các thư mục với phương pháp zip.

Hãy xem this page cho biết cách bạn có thể đệ quy đệ quy thư mục đã cho.

+0

Tôi nghĩ @dogbane là chính xác. Tôi đã chạy mã của bạn bằng cách sử dụng một thư mục chỉ chứa các tệp và nó hoạt động như dự định. Ngay sau khi tôi thêm một directoyr lồng nhau, tôi đã nhận FNF (Access bị từ chối) ngoại lệ. –

2

Tôi sẽ bao gồm ant task for zipping - đó là cách dễ dàng hơn để làm việc.

Lớp nhiệm vụ có thể được tìm thấy ở đây: org.apache.tools.ant.taskdefs.Zip (sử dụng chương trình khác)

+0

Bạn có thể đưa ra một số điểm về lý do tại sao chúng ta phải thích nhiệm vụ kiến ​​để nén không? – ivorykoder

+1

nó được thực hiện trong 3 dòng mã, và nó hoạt động. So sánh với phần trên. – Bozho

+0

@Bozho Tôi có thể tìm thấy nhiều tệp jar trong phiên bản gần đây của Ant. cái nào nên được sử dụng cho thư mục nén? – LGAP

0
private void zipFiles (ArrayList listWithFiles, String zipName) { 
    try { 

     byte[] buffer = new byte[1024]; 

     // create object of FileOutputStream 
     FileOutputStream fout = new FileOutputStream(zipName); 

     // create object of ZipOutputStream from FileOutputStream 
     ZipOutputStream zout = new ZipOutputStream(fout); 

     for (String currentFile : listWithFiles) { 

      // create object of FileInputStream for source file 
      FileInputStream fin = new FileInputStream(currentFile); 

      // add files to ZIP 
      zout.putNextEntry(new ZipEntry(currentFile)); 

      // write file content 
      int length; 

      while ((length = fin.read(buffer)) > 0) { 
       zout.write(buffer, 0, length); 
      } 

      zout.closeEntry(); 

      // close the InputStream 
      fin.close(); 
     } 

     // close the ZipOutputStream 
     zout.close(); 
    } catch (IOException ioe) { 
     System.out.println("IOException :" + ioe); 
    } 
} 
+1

Nó không giải quyết được vấn đề của thư mục con – Comencau

18

Đây là mã để tạo lưu trữ ZIP. Kho lưu trữ được tạo sẽ giữ nguyên cấu trúc thư mục gốc (nếu có).

public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName) throws Exception { 
    if (fileToZip == null || !fileToZip.exists()) { 
     return; 
    } 

    String zipEntryName = fileToZip.getName(); 
    if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) { 
     zipEntryName = parrentDirectoryName + "/" + fileToZip.getName(); 
    } 

    if (fileToZip.isDirectory()) { 
     System.out.println("+" + zipEntryName); 
     for (File file : fileToZip.listFiles()) { 
      addDirToZipArchive(zos, file, zipEntryName); 
     } 
    } else { 
     System.out.println(" " + zipEntryName); 
     byte[] buffer = new byte[1024]; 
     FileInputStream fis = new FileInputStream(fileToZip); 
     zos.putNextEntry(new ZipEntry(zipEntryName)); 
     int length; 
     while ((length = fis.read(buffer)) > 0) { 
      zos.write(buffer, 0, length); 
     } 
     zos.closeEntry(); 
     fis.close(); 
    } 
} 

Đừng quên đóng luồng đầu ra sau khi gọi phương thức này. Dưới đây là ví dụ:

public static void main(String[] args) throws Exception { 
    FileOutputStream fos = new FileOutputStream("C:\\Users\\vebrpav\\archive.zip"); 
    ZipOutputStream zos = new ZipOutputStream(fos); 
    addDirToZipArchive(zos, new File("C:\\Users\\vebrpav\\Downloads\\"), null); 
    zos.flush(); 
    fos.flush(); 
    zos.close(); 
    fos.close(); 
} 
0

Đây là những gì tôi đã viết. Ví dụ này giữ cấu trúc của các tệp và bằng cách đó, tránh ngoại lệ nhập trùng lặp.

/** 
    * Compress a directory to ZIP file including subdirectories 
    * @param directoryToCompress directory to zip 
    * @param outputDirectory  where to place the compress file 
    */ 
    public void zipDirectory(File directoryToCompress, File outputDirectory){ 
     try { 
      FileOutputStream dest = new FileOutputStream(new File(outputDirectory, directoryToCompress.getName() + ".zip")); 
      ZipOutputStream zipOutputStream = new ZipOutputStream(dest); 

      zipDirectoryHelper(directoryToCompress, directoryToCompress, zipOutputStream); 
      zipOutputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
    } 

    private void zipDirectoryHelper(File rootDirectory, File currentDirectory, ZipOutputStream out) throws Exception { 
     byte[] data = new byte[2048]; 

     File[] files = currentDirectory.listFiles(); 
     if (files == null) { 
      // no files were found or this is not a directory 

     } else { 
      for (File file : files) { 
       if (file.isDirectory()) { 
        zipDirectoryHelper(rootDirectory, file, out); 
       } else { 
        FileInputStream fi = new FileInputStream(file); 
        // creating structure and avoiding duplicate file names 
        String name = file.getAbsolutePath().replace(rootDirectory.getAbsolutePath(), ""); 

        ZipEntry entry = new ZipEntry(name); 
        out.putNextEntry(entry); 
        int count; 
        BufferedInputStream origin = new BufferedInputStream(fi,2048); 
        while ((count = origin.read(data, 0 , 2048)) != -1){ 
         out.write(data, 0, count); 
        } 
        origin.close(); 
       } 
      } 
     } 

    } 
Các vấn đề liên quan