Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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压缩目录时获取奇怪的结构文件_Java_Zipfile - Fatal编程技术网

使用Java压缩目录时获取奇怪的结构文件

使用Java压缩目录时获取奇怪的结构文件,java,zipfile,Java,Zipfile,我想压缩一个包含文件和子目录的目录。我做到了这一点,并且工作得很好,但是我得到了一个不寻常和奇怪的文件结构(至少我是这样认为的) 这是创建的文件:当我单击它时,我会看到一个“空”目录,如下所示:但当我解压缩它时,我会看到这个文件结构(并非所有名称都如下图所示): 我的问题是,“mantenimiento”文件夹是我要压缩的地方(我要压缩的目录),我不希望它在那里,所以当我解压缩刚创建的.zip文件时,我希望它具有这种文件结构(即“mantenimiento”目录中的文件和目录):另一件事是,当我

我想压缩一个包含文件和子目录的目录。我做到了这一点,并且工作得很好,但是我得到了一个不寻常和奇怪的文件结构(至少我是这样认为的)

这是创建的文件:当我单击它时,我会看到一个“空”目录,如下所示:但当我解压缩它时,我会看到这个文件结构(并非所有名称都如下图所示):

我的问题是,“mantenimiento”文件夹是我要压缩的地方(我要压缩的目录),我不希望它在那里,所以当我解压缩刚创建的.zip文件时,我希望它具有这种文件结构(即“mantenimiento”目录中的文件和目录):另一件事是,当我单击.zip文件时,我希望看到文件和目录,就像上面显示的图像一样

我不知道我的代码出了什么问题,我已经搜索过了,但没有找到我的问题所在的参考

这是我的密码:

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();
    }

}
private void zipFiles(列表文件、字符串目录)引发IOException
{
ZipOutputStream zos=null;
ZipEntry ZipEntry=null;
FileInputStream fin=null;
FileOutputStream=null;
BufferedInputStream in=null;
字符串zipFileName=getZipFileName();
尝试
{
fos=新文件输出流(File.separatorChar+zipFileName+扩展名);
zos=新ZipoutStream(fos);
字节[]buf=新字节[1024];
内伦;
用于(文件:文件)
{
zipEntry=新的zipEntry(file.toString());
fin=新文件输入流(文件);
in=新的BufferedInputStream(fin);
佐斯·普特内森特里(齐彭特里);
而((len=in.read(buf))>=0)
{
写(buf,0,len);
}
}
}
捕获(例外e)
{
系统错误println(“没有可能的归档文件”);
e、 printStackTrace();
}
最后
{
in.close();
zos.closeEntry();
zos.close();
}
}
希望你们能给我一个提示,告诉我我做错了什么或者我错过了什么

非常感谢


顺便说一句,我给这个方法的目录从未被使用过。我给出的另一个参数是文件列表,其中包含C:\mantenimiento目录中的所有文件和目录。

我曾经遇到windows和zip文件的问题,其中创建的zip不包含文件夹的条目(即
/
/ru毯a
等),仅包含文件条目。尝试为没有流式内容的文件夹添加ZipEntries

但是,作为Java中有点笨重的Zip API的替代,您可以使用文件系统(自Java7以来)。以下示例适用于Java8(lambda):

//Path pathToZip=Path.get(“Path/to/your/folder”);
//Path zipFile=Path.get(“file.zip”);
公共路径zipPath(路径pathToZip,路径zipFile){
Map env=new HashMap(){{
放置(“创建”、“真实”);
}};
try(FileSystem zipFs=FileSystems.newFileSystem(URI.create(“jar:+zipFile.toUri()),env)){
路径根=zipFs.getPath(“/”;
Files.walk(pathToZip.forEach(path->zip(root,path));
}
}
私有静态void zip(最终路径zipRoot,最终路径currentPath){
Path entryPath=zipRoot.resolve(currentPath.toString());
试一试{
Files.createDirectories(entryPath.getParent());
复制(currentPath,entryPath);
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}

我曾经遇到windows和zip文件的问题,创建的zip不包含文件夹条目(即
/
/ruea
等),仅包含文件条目。尝试为没有流式内容的文件夹添加ZipEntries

但是,作为Java中有点笨重的Zip API的替代,您可以使用文件系统(自Java7以来)。以下示例适用于Java8(lambda):

//Path pathToZip=Path.get(“Path/to/your/folder”);
//Path zipFile=Path.get(“file.zip”);
公共路径zipPath(路径pathToZip,路径zipFile){
Map env=new HashMap(){{
放置(“创建”、“真实”);
}};
try(FileSystem zipFs=FileSystems.newFileSystem(URI.create(“jar:+zipFile.toUri()),env)){
路径根=zipFs.getPath(“/”;
Files.walk(pathToZip.forEach(path->zip(root,path));
}
}
私有静态void zip(最终路径zipRoot,最终路径currentPath){
Path entryPath=zipRoot.resolve(currentPath.toString());
试一试{
Files.createDirectories(entryPath.getParent());
复制(currentPath,entryPath);
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}

能否共享getZipFileName方法。该方法实现非常简单:私有字符串getZipFileName方法(){String zipName;日期currentDate=newDate();SimpleDateFormat formatter=SimpleDateFormat(“yyyy_MM_dd_HH-MM-ss”);zipName=formatter.format(currentDate);return zipName;}能否共享getZipFileName方法。该方法实现非常简单:私有字符串getZipFileName方法(){String zipName;Date currentDate=newDate();SimpleDateFormat formatter=SimpleDateFormat(“yyyy_-MM_-dd_HH-MM-ss”);zipName=formatter.format(currentDate);return zipName;}我想按照您的建议在Java7中试用,但遗憾的是,我必须在Java6中使用。无论如何,我将尝试这种方法“尝试为没有流式内容的文件夹添加ZipPentries”。尝试在windows中创建一个zip,用7zip之类的工具或直接用java打开它并列出条目。如果我没有完全错的话,应该有表示路径的条目,或者它们以a/之类的开头。但是,在创建zip条目的方式和windows处理它的方式(或不处理)之间肯定存在联系
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();
    }

}
//Path pathToZip = Paths.get("path/to/your/folder");
//Path zipFile = Paths.get("file.zip");

public Path zipPath(Path pathToZip, Path zipFile) {
    Map<String, String> env = new HashMap<String, String>() {{
        put("create", "true");
    }};

    try (FileSystem zipFs = FileSystems.newFileSystem(URI.create("jar:" + zipFile.toUri()), env)) {
        Path root = zipFs.getPath("/");
        Files.walk(pathToZip).forEach(path -> zip(root, path));
    }
}

private static void zip(final Path zipRoot, final Path currentPath) {

    Path entryPath = zipRoot.resolve(currentPath.toString());
    try {
        Files.createDirectories(entryPath.getParent());
        Files.copy(currentPath, entryPath);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}