压缩文件中显示的文件路径
我有一个如下的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为:
f.getPath()
将在字符串的开头返回Output/
。这是由于程序的流程及其(错误)使用 File 对象的方式造成的。我建议您构造一个名为
tmp
的 File 对象:更改
f
的构造:然后,更改此:
对此:
我实际上没有时间测试它,但简而言之,您的问题是由于 File 对象的构造方式造成的。
It happens because of this:
f.getPath()
will returnOutput/
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
:The change the construction of
f
:Then, change this:
To this:
I didn't have time to actually test it, but in a nutshell, your problem is due to how the File object is constructed.