我想压缩包含文件和子目录的目录。我做到了这一点,工作得很好,但我越来越不寻常和好奇的文件结构(至少我看到了这种方式)。使用Java压缩目录时获取奇怪的结构文件
这是创建的文件:当我点击它,我看到这样一个“空”目录:但是当我解压这个我看到这个文件结构(不是所有的名称是exacly,因为它们是在显示下图):
|mantenimiento
|Carpeta_A
|File1.txt
|File2.txt
|Carpeta_B
|Sub_carpetaB
|SubfileB.txt
|Subfile1B.txt
|Subfile2B.txt
|File12.txt
我的问题在某种程度上是该文件夹“mantenimiento”就是我从(我要压缩的目录)zippping,我不希望它在那里,所以当我解压刚刚创建的.zip文件我想要它与这个文件结构(这是“mantenimiento”目录内的文件和目录):另一件事是当我点击.zip文件我想看到文件nd目录就像上面显示的图像一样。
我不知道我的代码有什么问题,我搜索了但没有找到我的问题可能的参考。
这里是我的代码:
private void zipFiles(List<File> files, String directory) throws IOException
{
ZipOutputStream zos = null;
ZipEntry zipEntry = null;
FileInputStream fin = null;
FileOutputStream fos = null;
BufferedInputStream in = null;
String zipFileName = getZipFileName();
try
{
fos = new FileOutputStream(File.separatorChar + zipFileName + EXTENSION);
zos = new ZipOutputStream(fos);
byte[] buf = new byte[1024];
int len;
for(File file : files)
{
zipEntry = new ZipEntry(file.toString());
fin = new FileInputStream(file);
in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);
while ((len = in.read(buf)) >= 0)
{
zos.write(buf, 0, len);
}
}
}
catch(Exception e)
{
System.err.println("No fue posible zipear los archivos");
e.printStackTrace();
}
finally
{
in.close();
zos.closeEntry();
zos.close();
}
}
希望你们能给我什么,我做错了,或我错过了什么暗示。
非常感谢。
顺便说一句,我给该方法的目录从来没有使用过。我给出的另一个参数是包含C:\ mantenimiento目录中所有文件和目录的文件列表。
您能分享getZipFileName方法吗? – Sanj
该方法实现非常简单:private String getZipFileName method() { \t String zipName; \t \t Date currentDate = newDate(); \t \t SimpleDateFormat formatter = SimpleDateFormat(“yyyy_MM_dd_HH-mm-ss”); \t \t zipName = formatter.format(currentDate); \t \t return zipName; \t } –