在Java中使用文件创建结构化存档

发布于 2025-02-13 18:07:59 字数 1087 浏览 1 评论 0原文

我想实现返回zip文件的API。

我使用OpenAPI定义了API

openapi: 3.0.1
info:
  title: xxx
  description: xxx
  version: 2.0.0
tags:
  - name: xxxx
servers:
  - url: /cc/api/v3
paths:
  /path/{param}:
    get:
      tags:
        - xxxx
      operationId: getMyZip
      responses:
        200:
          description: file with zipped reports
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
      security: []

据我了解,

@Override
  public ResponseEntity<Resource> getMyZip(String param) {
    return ResponseEntity
      .ok()
      .contentType(MediaType.APPLICATION_OCTET_STREAM)
      .body(new ByteArrayResource(myService.getMyZip(param)));
  }

,据我了解我的API应该返回字节[],因此我的控制器看起来可以返回字节[],否则应该是一些流,最终用户将获得zip?

实现。

我有使用杰克逊对象映射器解析的对象图。

如何创建文件夹结构并使用对象映射器输出JSON输出的文件。拉链并返回来电。

示例:

myZip.zip
   File1.json
   File2.json
   folder1
   folder2
     File3.json

I want to implement GET api that returns ZIP file.

I defined api using openapi

openapi: 3.0.1
info:
  title: xxx
  description: xxx
  version: 2.0.0
tags:
  - name: xxxx
servers:
  - url: /cc/api/v3
paths:
  /path/{param}:
    get:
      tags:
        - xxxx
      operationId: getMyZip
      responses:
        200:
          description: file with zipped reports
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
      security: []

As far as I understand my api should return byte[], so my controller looks like below

@Override
  public ResponseEntity<Resource> getMyZip(String param) {
    return ResponseEntity
      .ok()
      .contentType(MediaType.APPLICATION_OCTET_STREAM)
      .body(new ByteArrayResource(myService.getMyZip(param)));
  }

Is it ok to return byte[] or it should be some stream, end user will get the zip?

Implementation.

I have map of objects that I am parsing using Jackson object mapper.

How I could create folder structure and place files with json outputs from object mapper. Zip it and return to the caller.

Example:

myZip.zip
   File1.json
   File2.json
   folder1
   folder2
     File3.json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-02-20 18:07:59

只需使用zipoutputstream,然后将其包裹在bytearrayoutputstream上:

ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(bStream);

ZipEntry myEntry = ZipEntry("my/awesome/folder/file.txt");

zipStream.addEntry(myEntry);
zipStream.write(myAwesomeData);
//add and write more entries
zipStream.close();

byte[] result = bStream.toByteArray();

Just use ZipOutputStream, and wrap it around a ByteArrayOutputStream:

ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(bStream);

ZipEntry myEntry = ZipEntry("my/awesome/folder/file.txt");

zipStream.addEntry(myEntry);
zipStream.write(myAwesomeData);
//add and write more entries
zipStream.close();

byte[] result = bStream.toByteArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文