如何从 ByteArrayOutputStream 创建 java.io.File?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3978103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:14:35  来源:igfitidea点击:

How to create a java.io.File from a ByteArrayOutputStream?

javafile-io

提问by Tommy

I'm reading a bunch of files from an FTP. Then I need to unzip those files and write them to a fileshare.

我正在从 FTP 读取一堆文件。然后我需要解压缩这些文件并将它们写入文件共享。

I don't want to write the files first and then read them back and unzip them. I want to do it all in one go. Is that possible?

我不想先写入文件,然后再读回并解压缩它们。我想一口气完成这一切。那可能吗?

This is my code

这是我的代码

FTPClient fileclient = new FTPClient();
..

ByteArrayOutputStream out = new ByteArrayOutputStream();
fileclient.retrieveFile(filename, out);

??????? //How do I get my out-stream into a File-object? 

File file = new File(?);

ZipFile zipFile = new ZipFile(file,ZipFile.OPEN_READ);

Any ideas?

有任何想法吗?

回答by Qwerky

You should use a ZipInputStreamwrapped around the InputStreamreturned from FTPClient's retrieveFileStream(String remote).

您应该使用ZipInputStream环绕InputStreamFTPClient's返回的retrieveFileStream(String remote).

回答by Hyman

You don't need to create the Fileobject.

您不需要创建File对象。

If you want to save the file you should pipe the stream directly into a ZipOutputStream

如果您想保存文件,您应该将流直接通过管道传输到 ZipOutputStream

ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(out);

// do whatever with your zip file

If, instead, you want to open the just retrieved file work with the ZipInputStream:

相反,如果您想打开刚刚检索到的文件,请使用ZipInputStream

new ZipInputStream(fileClient.retrieveFileStream(String remote));

Just read the doc hereand here

只需在这里这里阅读文档

回答by Dave Costa

I think you want:

我想你想要:

ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream( out.toByteArray() ) );

Then read your data from the ZipInputStream.

然后从 ZipInputStream 读取您的数据。

回答by Jon Skeet

Well, you could just create a FileOutputStreamand then write the data from that:

好吧,您可以创建一个FileOutputStream,然后从中写入数据:

FileOutputStream fos = new FileOutputStream(filename);
try {
  out.writeTo(fos);
} finally {
  fos.close();
}

Then just create the Fileobject:

然后只需创建File对象:

File file = new File(filename);

You need to understand that a Fileobject doesn't represent any real data on disk - it's just a filename, effectively. The file doesn't even have to exist. If you want to actually write data, that's what FileOutputStreamis for.

您需要了解File对象并不代表磁盘上的任何真实数据——它实际上只是一个文件名。该文件甚至不必存在。如果您想真正写入 data,那FileOutputStream就是。

EDIT: I've just spotted that you didn't want to write the data out first - but that's what you've gotto do, if you're going to pass the file to something that expects a genuine file with data in.

编辑:我刚刚发现,你不想写出来第一数据-但是,这是你什么做的,如果你要的文件传递的东西,希望在数据真正的文件。

If you don't want to do that, you'll have to use a different API which doesn'texpect a file to exist... as per Qwerky's answer.

如果您不想这样做,则必须使用不同的 API,该 API希望文件存在......根据 Qwerky 的回答。

回答by Stephen C

As others have pointed out, for what you are trying to do, you don't need to write the downloaded ZIP "file" to the file system at all.

正如其他人指出的那样,对于您要执行的操作,您根本不需要将下载的 ZIP“文件”写入文件系统。

Having said that, I'd like to point out a misconception in your question, that is also reflected in some of the answers.

话虽如此,我想指出您问题中的一个误解,这也反映在一些答案中。

In Java, a Fileobject does no really represent a file at all. Rather, it represents a file nameor *path". While this name or path often corresponds to an actual file, this doesn't need to be the case.

在 Java 中,File对象根本不代表文件。相反,它代表一个文件或“*path”。虽然这个名称或路径通常对应于一个实际的文件,但不一定是这样。

This may sound a bit like hair-splitting, but consider this scenario:

这听起来有点像头​​发分裂,但请考虑这种情况:

File dir = new File("/tmp/foo");
boolean isDirectory = dir.isDirectory();
if (isDirectory) {
    // spend a long time computing some result
    ...
    // create an output file in 'dir' containing the result
}

Now if instances of the Fileclass represented objects in the file system, then you'd expect the code that creates the output file to succeed (modulo permissions). But in fact, the create could fail because, something deleted the "/tmp/foo", or replaced it with a regular file.

现在,如果File类的实例表示文件系统中的对象,那么您会期望创建输出文件的代码成功(模权限)。但实际上,创建可能会失败,因为某些东西删除了“/tmp/foo”,或将其替换为常规文件。

It must be said that someof the methods on the Fileclass do seem to assume that the Fileobject does correspond to a real filesystem entity. Examples are the methods for getting a file's size or timestamps, or for listing the names in a directory. However, in each case, the method is specified to throw an exception if the actual file does not exist or has the wrong type for the operation requested.

必须指出的是,类上的某些方法File似乎File确实假设该对象确实对应于一个真实的文件系统实体。示例是获取文件大小或时间戳的方法,或列出目录中名称的方法。但是,在每种情况下,如果实际文件不存在或请求的操作类型错误,则该方法被指定为抛出异常。

回答by user207421

Just change the ByteArrayOutputStream to a FileOutputStream.

只需将 ByteArrayOutputStream 更改为 FileOutputStream。