压缩文件中显示的文件路径

发布于 2024-09-29 00:26:47 字数 1580 浏览 6 评论 0原文

我有一个如下的java程序,用于将文件夹作为一个整体进行压缩。

public static void zipDir(String dir2zip, ZipOutputStream zos) 

{ 

try 

{ 

           File zipDir= new File(dir2zip); 

            String[] dirList = zipDir.list(); 

          byte[] readBuffer = new byte[2156]; 

          int bytesIn = 0; 

      for(int i=0; i<dirList.length; i++) 

       { 

         File f = new File(zipDir, dirList[i]); 

      if(f.isDirectory()) 

      { 

                    String filePath = f.getPath(); 

           zipDir(filePath, zos); 

         continue; 

     } 

          FileInputStream fis = new FileInputStream(f); 

            ZipEntry anEntry = new ZipEntry(f.getPath()); 

                zos.putNextEntry(anEntry); 

       while((bytesIn = fis.read(readBuffer)) != -1) 

       { 

           zos.write(readBuffer, 0, bytesIn); 

        } 

      fis.close(); 

    } 

} 

catch(Exception e) 

{ 

 e.printStackTrace();

} 


}

public static void main(){
String date=new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.Date());

         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("Output/" + date + "_RB" + ".zip"));

         zipDir("Output/" + date + "_RB", zos); 

         zos.close();
}

我的查询是。要压缩的目标文件夹(+日期+_RB)位于名为 Output 的文件夹内。成功压缩后,当我提取压缩文件时,我找到一个文件夹 Output,其中存在 (+date+_RB) 所需的文件夹。在提取压缩文件后,我不需要输出文件夹,而应该直接单独提取所需的文件夹。请就此提出建议。

更新:

我尝试了艾萨克的回答。提取生成的 zip 文件时,不会提取任何文件夹。仅提取所有文件夹内的文件。我只是不需要在生成的 zip 文件中单独使用“Output”文件夹。但该程序所做的是,它不会提取输出文件夹内的所有其他文件夹,而只是提取这些文件夹内的文件。请告知如何继续...

I have a java program as below for zipping a folder as a whole.

public static void zipDir(String dir2zip, ZipOutputStream zos) 

{ 

try 

{ 

           File zipDir= new File(dir2zip); 

            String[] dirList = zipDir.list(); 

          byte[] readBuffer = new byte[2156]; 

          int bytesIn = 0; 

      for(int i=0; i<dirList.length; i++) 

       { 

         File f = new File(zipDir, dirList[i]); 

      if(f.isDirectory()) 

      { 

                    String filePath = f.getPath(); 

           zipDir(filePath, zos); 

         continue; 

     } 

          FileInputStream fis = new FileInputStream(f); 

            ZipEntry anEntry = new ZipEntry(f.getPath()); 

                zos.putNextEntry(anEntry); 

       while((bytesIn = fis.read(readBuffer)) != -1) 

       { 

           zos.write(readBuffer, 0, bytesIn); 

        } 

      fis.close(); 

    } 

} 

catch(Exception e) 

{ 

 e.printStackTrace();

} 


}

public static void main(){
String date=new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.Date());

         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("Output/" + date + "_RB" + ".zip"));

         zipDir("Output/" + date + "_RB", zos); 

         zos.close();
}

My query here is. The target folder(+date+_RB) to be zipped is present inside the folder named Output. After successful zipping, when I extract the zipped file, I find a folder Output inside which the (+date+_RB) required folder is present. I need not want that Output folder after the extraction of the zipped file, rather it should directly extract the required folder alone. Please advise on the same.

UPDATE:

I tried Isaac's answer. While extracting the resultant zip file, no folders are getting extracted. Only the files inside all the folders are getting extracted. I just dont need the folder "Output" alone in the resultant zip file. But what the program does is, it doesnt extracts all other folders inside the Output folder, rather it just extracts the files inside those folders. Kindly advise on how to proceed...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-10-06 00:26:47

发生这种情况是因为:

ZipEntry anEntry = new ZipEntry(f.getPath());

f.getPath() 将在字符串的开头返回 Output/。这是由于程序的流程及其(错误)使用 File 对象的方式造成的。

我建议您构造一个名为 tmp 的 File 对象:

File tmp = new File(dirList[i]);

更改 f 的构造:

File f = new File(zipDir, tmp.getPath());

然后,更改此:

ZipEntry anEntry = new ZipEntry(f.getPath());

对此:

ZipEntry anEntry = new ZipEntry(tmp.getPath());

我实际上没有时间测试它,但简而言之,您的问题是由于 File 对象的构造方式造成的。

It happens because of this:

ZipEntry anEntry = new ZipEntry(f.getPath());

f.getPath() will return Output/ at the beginning of the string. This is due to the flow of your program and how it (mis)uses File objects.

I suggest you construct a File object called, say, tmp:

File tmp = new File(dirList[i]);

The change the construction of f:

File f = new File(zipDir, tmp.getPath());

Then, change this:

ZipEntry anEntry = new ZipEntry(f.getPath());

To this:

ZipEntry anEntry = new ZipEntry(tmp.getPath());

I didn't have time to actually test it, but in a nutshell, your problem is due to how the File object is constructed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文