2013-02-25 88 views
1

我有以下文件夹结构:邮编根文件夹的内容,而不在压缩文件具有根

 
RootFolder 
| 
| 
| -->F1-->F1.1-->t1.txt,t2.txt 
    -->F2-->F2.2-->t3.txt 

我已经成功了,使用下面的代码,得到以下zip文件:

结果.zip文件 - >包含:

 
RootFolder 
| 
| 
| -->F1-->F1.1-->t1.txt,t2.txt 
    -->F2-->F2.2-->t3.txt 

我需要创建一个具有无需创建根文件夹“RootFolder”整个“RootFolder”内容的zip文件;

我的意思是,我需要的结果是这样的:

result.zip - >包含:

 
| 
| 
| -->F1-->F1.1-->t1.txt,t2.txt 
    -->F2-->F2.2-->t3.txt 
public static void main(String[] args) throws Exception { 
    zipFolder("c:/new/RootFolder", "c:/new/result.zip"); 
} 


static public void zipFolder(String srcFolder, String destZipFile) 
throws IOException, FileNotFoundException { 
    ZipOutputStream zip = null; 
    FileOutputStream fileWriter = null; 

    fileWriter = new FileOutputStream(destZipFile); 
    zip = new ZipOutputStream(fileWriter); 

    addFolderToZip("", srcFolder, zip); 
    zip.flush(); 
    zip.close(); 
} 

static private void addFileToZip(String path, String srcFile, 
     ZipOutputStream zip) throws IOException, FileNotFoundException { 

    File folder = new File(srcFile); 
    if (folder.isDirectory()) { 
     addFolderToZip(path, srcFile, zip); 
    } else { 
     byte[] buf = new byte[1024]; 
     int len; 
     FileInputStream in = new FileInputStream(srcFile); 
     zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); 
     while ((len = in.read(buf)) > 0) { 
      zip.write(buf, 0, len); 
     } 
     in.close(); 
    } 
} 

    static public void addFolderToZip(String path, String srcFolder, 
      ZipOutputStream zip) throws IOException, FileNotFoundException { 
     File folder = new File(srcFolder); 

     for (String fileName : folder.list()) 

{ 
     if (path.equals("")) { 
      addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); 
     } else { 
      addFileToZip(path + "/" + folder.getName(), srcFolder + "/" 
        + fileName, zip); 
     } 
    } 
} 

回答

1

你需要做的是添加文件开始不与你根文件夹,但其内容。

喜欢的东西:

filelist = getFileList(rootFolder) 
foreach(File f : filelist){ 
    addFolderToZip(f) 
} 

对不起,伪代码,不记得了原有的功能名称和现在不能检查,但他们可以很容易地用Google搜索。

重点是只跳过在根文件夹的存档中创建一个文件夹。

+0

我一直在努力这样做,但每次我最终都会遇到这种凌乱算法的例外。 – Echo 2013-02-25 12:21:21

+0

@Echo,什么例外?显示你想要做的事情。 – svz 2013-02-25 12:24:07

+0

:我试图找到最简单的解决方案:) – Echo 2013-02-25 13:05:32

1

我已经写了一些实用的方法来目录复制到使用NIO文件API Zip文件(该库是开源的):

Maven的:

<dependency> 
    <groupId>org.softsmithy.lib</groupId> 
    <artifactId>softsmithy-lib-core</artifactId> 
    <version>0.3</version> 
</dependency> 

教程:

http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#AddZipResourceSample

API:CopyFileVisitor.copy

+0

我读过它,它很好。但是,根据我的阅读,它将容器文件夹放在zip文件中。我是对的! – Echo 2013-02-25 13:08:49

+0

我解决了我的问题,正如我在下面陈述的。感谢Puce – Echo 2013-02-25 13:09:16

+1

不,如果您在示例中省略了两行代码,请注意“将src目录名称添加到zip中,如果您只想复制内容。” – Puce 2013-02-25 13:27:34

0

我试图找出最简单的方法来解决我的问题。 我解决它可以只删除根目录名,而使用在zip文件保存如下:

String pathAfterOmittingtheRootFolder=path.replace(ROOT_FOLDER_NAME, ""); 

完整的方法是:

static private void addFileToZip(String path, String srcFile, 
     ZipOutputStream zip,String exportedRootDirectory) throws IOException, FileNotFoundException { 

    File folder = new File(srcFile); 
    if (folder.isDirectory()) { 

    addFolderToZip(path, srcFile, zip,exportedRootDirectory); 
} else { 
    byte[] buf = new byte[1024]; 
    int len; 
    FileInputStream in = new FileInputStream(srcFile); 
    String pathAfterOmittingtheRootFolder=path.replaceFirst(exportedRootDirectory, ""); 
    zip.putNextEntry(new ZipEntry(pathAfterOmittingtheRootFolder + "/" + folder.getName())); 
    while ((len = in.read(buf)) > 0) { 
     zip.write(buf, 0, len); 
    } 
    in.close(); 
} 

}