在Java中从ZipOutputStream返回Zip文件

3

我有一个函数,可以从文件列表中创建Zip文件。是否可能在不将其保存到磁盘的情况下返回Zip文件? 我需要该文件作为另一个函数的参数。 我不确定ByteStream是否适合我。

public File compressFileList(List<File> fileList,String fileName) {
    FileOutputStream fileOutputStream=null;
    ZipOutputStream zipOutputStream=null;
    FileInputStream fileInputStream=null;
    String compressedFileName=fileName +".zip";
    if(fileList.isEmpty())
        return null;
    try
    {
        fileOutputStream =  new FileOutputStream(compressedFileName);
        zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
        for (File file: fileList) {
            fileInputStream = new FileInputStream(file);
            ZipEntry zipEntry =  new ZipEntry(file.getName());
            zipOutputStream.putNextEntry(zipEntry);
            byte[] tmp = new byte[4*1024];
            int size = 0;
            while((size = fileInputStream.read(tmp)) != -1){
                zipOutputStream.write(tmp, 0, size);
            }
            zipOutputStream.flush();
            fileInputStream.close();
        }
        zipOutputStream.close();
        return compressedFile; //This is what I am missing

    }
    catch (FileNotFoundException e)
    {

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

编辑:添加用例

这个想法是创建一个zip文件,并使用Watson的VisualRecognition服务的CreateClassifierOptions方法。

classifierOptions = new CreateClassifierOptions.Builder()
            .classifierName("Santa")
            .addClass("Santa", new File("C:\\app\\GitRepo\\images\\beagle.zip"))
            .negativeExamples(new File("C:\\app\\GitRepo\\images\\nosport.zip"))
            .build();

建造者接受 zip 文件作为参数。

理解

根据 Alexandre Dupriez 的解释,我认为最好将文件存储在硬盘上的某个位置。


1
其他函数接受什么作为输入?是“Inputstream”吗? - Lino
我正在使用WatsonService API,它接受一个zip文件作为输入。或者也许我应该创建一个临时的zip文件并将其返回给函数。 - Vini
你的问题与方法签名不符。File对象基本上是对存储在某个路径中的文件的引用,但你的问题似乎是关于返回文件(字节)内容的。因此,如果你不想将文件存储在某处,那么就不应该返回一个File对象。 - Rob Obdeijn
我应该在磁盘上创建一个临时文件并将其发送到函数吗? - Vini
2
从功能的角度来看,你想要实现什么?存储一个临时文件并返回对它的引用是一个选择,但这取决于你对该文件想要做什么。 - Rob Obdeijn
我想使用Watson Visual API。Visual API使用一个zip文件作为参数。我决定将文件存储在临时文件夹中,并使用它来调用Visual API。 - Vini
2个回答

4

您可以使用ByteArrayOutputStream代替FileOutputStream

zipOutputStream = new ZipOutputStream(new ByteArrayOutputStream());

这里的难点在于向消费zip文件的方法提供一个“File”。java.io.File没有提供一个抽象,允许您操作内存中的文件。
java.io.File抽象和java.io.FileInputStream实现
简单来说,如果我们必须简化什么是“File”抽象,我们会发现它是一个URI。因此,为了能够构建一个内存中的“File”,或者至少模拟它,我们需要提供一个URI,然后由“File”的使用者使用它来读取其内容。
如果我们看一下使用者可能使用的“FileInputStream”,我们可以看到它总是以本地调用结束,这使我们无法对内存中的文件抽象出FileSystem。
// class java.io.FileInputStream
/**
 * Opens the specified file for reading.
 * @param name the name of the file
 */
private native void open0(String name) throws FileNotFoundException;

如果有一种可能性能够使消费者接受一个InputStream,那么这将会更容易,但基于您的问题陈述,我猜测这是不可能的。

API调用

您需要提供一个文件给Watson Visual API。请您提供所需调用的API方法?


我已经提到,我希望上述函数返回一个文件。调用函数只接受Zip文件作为输入。 - Vini
是的,请 - 想知道能做什么以及是否有解决方法。 - Alexandre Dupriez
我已经添加了使用案例。我认为最好先保存文件,然后调用Watson服务。 - Vini

1
public void compressFileList(List<File> fileList, OutputStream outputStream)
        throws IOException {
    try (ZipOutputStream zipOutputStream =
            new ZipOutputStream(new BufferedOutputStream(outputStream));
        for (File file: fileList) {
            try (FileInputStream fileInputStream = new FileInputStream(file)) {
                ZipEntry zipEntry = new ZipEntry(file.getName());
                zipOutputStream.putNextEntry(zipEntry);
                byte[] tmp = new byte[4*1024];
                int size = 0;
                while((size = fileInputStream.read(tmp)) != -1){
                    zipOutputStream.write(tmp, 0, size);
                }
                zipOutputStream.flush();
            } catch (FileNotFoundException e) { // Maybe skip not found files.
                Logger.log(Level.INFO, "File not found {}", file.getPath());
            }
        }
    }
}

使用方法:

if (fileList.isEmpty()) {
    ...
    return;
}
try {
    compressFileList(fileList, servletRequest.getOutputStream())) {
} catch (FileNotFoundException e) {
   ...
} catch (IOException e) {
    ...
}

它不返回文件。我正在寻找一个返回zip文件的选项。我已经意识到在将其用于下一个函数之前,我必须暂时保存该文件。 - Vini
或者(作为我的答案的int),您可以传递一个OutputStream来传递zip文件。然后,您无需维护一个包含zip文件内容的字节数组。 - Joop Eggen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接