Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 压缩根文件夹内容,而不将根放在Zip文件中_Java_Zip_Java Io_Apache Commons Io - Fatal编程技术网

Java 压缩根文件夹内容,而不将根放在Zip文件中

Java 压缩根文件夹内容,而不将根放在Zip文件中,java,zip,java-io,apache-commons-io,Java,Zip,Java Io,Apache Commons Io,我有以下文件夹结构: RootFolder | | | -->F1-->F1.1-->t1.txt,t2.txt -->F2-->F2.2-->t3.txt 您需要做的是添加不是从根文件夹开始的文件,而是从其内容开始的文件 比如: filelist = getFileList(rootFolder) foreach(File f : filelist){ addFolderToZip(f) } 对不起,伪代码,不记得原来的函数名,现在也不能检查它们,但是可以很容易地用谷歌搜

我有以下文件夹结构:

RootFolder | | | -->F1-->F1.1-->t1.txt,t2.txt -->F2-->F2.2-->t3.txt
您需要做的是添加不是从根文件夹开始的文件,而是从其内容开始的文件

比如:

filelist = getFileList(rootFolder)  
foreach(File f : filelist){  
    addFolderToZip(f)
}
对不起,伪代码,不记得原来的函数名,现在也不能检查它们,但是可以很容易地用谷歌搜索它们


要点是跳过在归档文件中为根文件夹创建文件夹。

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

马文:

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

org.softsmithy.lib


API:

我试图找到解决问题的最简单方法。 我通过以下方法解决了在保存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();
}

}

我一直在尝试这样做,但每次我都会遇到这样一个混乱算法的异常:我试图找到最简单的解决方案:)这里是:FileInputStream in=newfileinputstream(srcFile);字符串tmp=path.replace(根文件夹名称“”);因此,我所做的就是将前一行添加到addFileToZip方法静态私有void addFileToZip(String path,String srcFile,ZipOutputStream zip)抛出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);String tmp=path.replace(ROOT_FOLDER_NAME,”);zip.putnextery(new-ZipEntry(tmp+“/”+FOLDER.getName());while((len=in.read(buf))>0){zip.write(buf,0,len);}in.close();}我读了它,它很好。然而,根据我的阅读,它将容器文件夹也放在zip文件中。。我是对的!不,如果您省略示例中带有注释“将src目录名添加到zip中”的两行,则不应该这样做。如果您只想复制内容,则可以省略这一行
<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.3</version>  
</dependency>  
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();
}