java:更改ZipEntry的名字 [英] java: change ZipEntry name

查看:1856
本文介绍了java:更改ZipEntry的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FileOutputStream fOut = new FileOutputStream(fullFilename,false ); 
BufferedOutputStream bOut = new BufferedOutputStream(fOut);
ZipOutputStream zOut = new ZipOutputStream(bOut);

zOut.putNextEntry(new ZipEntry(aFile1.txt));
//做一些处理并写入zOut ...
zOut.write(...);
(...)
zOut.closeEntry();

zOut.close();
//等(关闭所有资源)

我需要更改文件名zipEntry被写入后(因为它的名字将取决于它的内容写入)。

另外,它不是写入缓冲区和写入文件的选项当最终的文件名是已知的(因为文件大小可能非常大:没有足够的内存)。

任何建议如何做到这一点将不胜感激!



谢谢,
Thomas

解决方案

可能是简单的,因为条目本身没有被压缩。

最简单的方法是需要重写,但是这个文件是 zip FileSystem :因为Java 7可以使用zip文件作为虚拟文件系统:写入,重命名和移动文件。 示例。您将文件从普通文件系统复制到zip文件系统,然后在zip中重命名该文件。

  //创建zip文件:
URI zipURI = URI.create(jar:file:+ fullFilename); //jar:file:/ ... / ... .zip
Map< String,Object> env = new HashMap<>();
env.put(create,true);
FileSystem zipFS = FileSystems.newFileSystem(zipURI,env,null);

//写入aFile1.txt:
路径pathInZipfile = zipFS.getPath(/ aFile1.txt);
BufferedWriter out = Files.newBufferedWriter(pathInZipfile,
StandardCharsets.UTF_8,StandardOpenOption.CREATE_NEW);
out.write(按下任意键,除两个shift键均为\外)
out.close();

//重命名文件:
路径pathInZipfile2 = zipFS.getPath(/ aFile2.txt);
Files.move(pathInZipfile,pathInZipfile2);

zipFS.close();

原则上,您也可以保留旧的代码 - 无需重命名。并使用一个zip文件系统来重命名。


I have the following code that writes a text file in a zip:

FileOutputStream fOut = new FileOutputStream(fullFilename, false);
BufferedOutputStream bOut = new BufferedOutputStream(fOut);
ZipOutputStream zOut = new ZipOutputStream(bOut);

zOut.putNextEntry(new ZipEntry("aFile1.txt"));
//Do some processing and write to zOut...
zOut.write(...);
(...)
zOut.closeEntry();

zOut.close();
//Etc (close all resources)

I would need to change the filename of the zipEntry after it has been written (as its name will depend on its content written).

Also, it is not an option to write in a buffer and write to file only when final filename is known (because file size is potentially very large: not enough memory).

Any advice on how to do this would be greatly appreciated!

Thanks, Thomas

解决方案

It is a missing functionality, which could have been simple, as the entries themselves are not compressed.

The easiest way, requiring a rewrite though, is the zip FileSystem: since java 7 you may use a zip file as a virtual file system: writing, renaming and moving files in them. An example. You copy a file from the normal file system into the zip file system, and later rename the file in the zip.

// Create the zip file:
URI zipURI = URI.create("jar:file:" + fullFilename); // "jar:file:/.../... .zip"
Map<String, Object> env = new HashMap<>(); 
env.put("create", "true");
FileSystem zipFS = FileSystems.newFileSystem(zipURI, env, null);

// Write to aFile1.txt:
Path pathInZipfile = zipFS.getPath("/aFile1.txt");
BufferedWriter out = Files.newBufferedWriter(pathInZipfile,
        StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
out.write("Press any key, except both shift keys\n");
out.close();

// Rename file:
Path pathInZipfile2 = zipFS.getPath("/aFile2.txt");
Files.move(pathInZipfile, pathInZipfile2);

zipFS.close();

In principle you could also keep your old code - without renaming. And use a zip file system just for renaming.

这篇关于java:更改ZipEntry的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆