|
26 | 26 | import org.springframework.data.domain.Sort; |
27 | 27 | import org.springframework.stereotype.Service; |
28 | 28 |
|
| 29 | +import java.io.ByteArrayInputStream; |
29 | 30 | import java.io.ByteArrayOutputStream; |
30 | 31 | import java.io.IOException; |
31 | 32 | import java.io.InputStream; |
32 | 33 | import java.time.LocalDateTime; |
33 | 34 | import java.util.List; |
34 | 35 | import java.util.UUID; |
35 | 36 | import java.util.stream.Collectors; |
| 37 | +import java.util.zip.ZipEntry; |
| 38 | +import java.util.zip.ZipInputStream; |
36 | 39 |
|
37 | 40 | @RequiredArgsConstructor |
38 | 41 | @Slf4j |
@@ -145,20 +148,33 @@ public void checkExpire(final LocalDateTime updatedAt, final ProductStatus produ |
145 | 148 | public byte[] downloadFile(final String fileKey) { |
146 | 149 | try (S3Object s3Object = amazonS3.getObject(bucket, fileKey); |
147 | 150 | InputStream inputStream = s3Object.getObjectContent(); |
148 | | - ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 151 | + ByteArrayOutputStream zipBuffer = new ByteArrayOutputStream()) { |
149 | 152 |
|
150 | | - // 버퍼를 사용하여 데이터 읽기 |
151 | 153 | byte[] buffer = new byte[4096]; |
152 | 154 | int bytesRead; |
153 | 155 | while ((bytesRead = inputStream.read(buffer)) != -1) { |
154 | | - outputStream.write(buffer, 0, bytesRead); |
| 156 | + zipBuffer.write(buffer, 0, bytesRead); |
155 | 157 | } |
156 | 158 |
|
157 | | - return outputStream.toByteArray(); |
158 | | - } catch (AmazonS3Exception e) { |
159 | | - throw new RuntimeException("S3에서 파일을 찾을 수 없습니다: " + fileKey); |
| 159 | + // ZIP 해제 |
| 160 | + try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBuffer.toByteArray()))) { |
| 161 | + ZipEntry entry = zipInputStream.getNextEntry(); |
| 162 | + |
| 163 | + if (entry != null) { |
| 164 | + ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream(); |
| 165 | + |
| 166 | + // ZIP 안의 실제 PDF 바이트만 꺼냄 |
| 167 | + while ((bytesRead = zipInputStream.read(buffer)) != -1) { |
| 168 | + pdfBuffer.write(buffer, 0, bytesRead); |
| 169 | + } |
| 170 | + |
| 171 | + return pdfBuffer.toByteArray(); |
| 172 | + } else { |
| 173 | + throw new RuntimeException("ZIP 파일 안에 PDF가 없습니다."); |
| 174 | + } |
| 175 | + } |
160 | 176 | } catch (IOException e) { |
161 | | - throw new RuntimeException("파일 다운로드 중 오류가 발생했습니다: " + fileKey); |
| 177 | + throw new RuntimeException("파일 다운로드 중 오류 발생: " + fileKey, e); |
162 | 178 | } |
163 | 179 | } |
164 | 180 |
|
|
0 commit comments