|
14 | 14 | import java.io.UnsupportedEncodingException; |
15 | 15 | import java.nio.charset.StandardCharsets; |
16 | 16 | import java.nio.file.FileSystem; |
17 | | -import java.nio.file.FileSystems; |
18 | 17 | import java.nio.file.Files; |
19 | 18 | import java.nio.file.Path; |
20 | 19 | import java.nio.file.Paths; |
21 | | -import java.nio.file.StandardCopyOption; |
22 | 20 | import java.sql.SQLException; |
23 | 21 | import java.util.ArrayList; |
24 | 22 | import java.util.Arrays; |
|
31 | 29 | import java.util.function.Function; |
32 | 30 | import java.util.regex.Matcher; |
33 | 31 | import java.util.regex.Pattern; |
| 32 | +import java.util.zip.ZipEntry; |
| 33 | +import java.util.zip.ZipInputStream; |
34 | 34 |
|
35 | 35 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
36 | 36 | import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
@@ -311,38 +311,40 @@ private void addFilePath(List<String> filePaths, String path, long size) { |
311 | 311 | filePaths.add(fileInfo); |
312 | 312 | } |
313 | 313 |
|
314 | | - /** |
315 | | - * Processes a TAR file, extracting its entries and adding their paths to the provided list. |
316 | | - * @param filePaths the list to populate with the extracted file paths |
317 | | - * @param tempFile the temporary TAR file to process |
318 | | - * @throws IOException if an I/O error occurs while reading the TAR file |
319 | | - */ |
320 | | - private void processTarFile(List<String> filePaths, Path tempFile) throws IOException { |
321 | | - try (InputStream fi = Files.newInputStream(tempFile); |
322 | | - TarArchiveInputStream tis = new TarArchiveInputStream(fi)) { |
| 314 | +// /** |
| 315 | +// * Processes a TAR file, extracting its entries and adding their paths to the provided list. |
| 316 | +// * @param filePaths the list to populate with the extracted file paths |
| 317 | +// * @param tempFile the temporary TAR file to process |
| 318 | +// * @throws IOException if an I/O error occurs while reading the TAR file |
| 319 | +// */ |
| 320 | + private void processTarFile(List<String> filePaths, InputStream inputStream) throws IOException { |
| 321 | + try (TarArchiveInputStream tis = new TarArchiveInputStream(inputStream)) { |
323 | 322 | TarArchiveEntry entry; |
324 | 323 | while ((entry = tis.getNextTarEntry()) != null) { |
| 324 | + // Add the file path and its size (from the TAR entry) |
325 | 325 | addFilePath(filePaths, entry.getName(), entry.getSize()); |
326 | 326 | } |
327 | 327 | } |
328 | 328 | } |
329 | 329 |
|
330 | | - /** |
331 | | - * Processes a ZIP file, extracting its entries and adding their paths to the provided list. |
332 | | - * @param filePaths the list to populate with the extracted file paths |
333 | | - * @param zipFileSystem the FileSystem object representing the ZIP file |
334 | | - * @throws IOException if an I/O error occurs while reading the ZIP file |
335 | | - */ |
336 | | - private void processZipFile(List<String> filePaths, FileSystem zipFileSystem) throws IOException { |
337 | | - Path root = zipFileSystem.getPath("/"); |
338 | | - Files.walk(root).forEach(path -> { |
339 | | - try { |
340 | | - long fileSize = Files.size(path); |
341 | | - addFilePath(filePaths, path.toString().substring(1), fileSize); |
342 | | - } catch (IOException e) { |
343 | | - log.error("An error occurred while getting the size of the zip file.", e); |
| 330 | +// /** |
| 331 | +// * Processes a ZIP file, extracting its entries and adding their paths to the provided list. |
| 332 | +// * @param filePaths the list to populate with the extracted file paths |
| 333 | +// * @param zipFileSystem the FileSystem object representing the ZIP file |
| 334 | +// * @throws IOException if an I/O error occurs while reading the ZIP file |
| 335 | +// */ |
| 336 | + private void processZipFile(List<String> filePaths, InputStream inputStream) throws IOException { |
| 337 | + try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) { |
| 338 | + ZipEntry entry; |
| 339 | + // Read each entry in the ZIP file |
| 340 | + while ((entry = zipInputStream.getNextEntry()) != null) { |
| 341 | + if (!entry.isDirectory()) { |
| 342 | + // For each file in the ZIP, you can get its size and path |
| 343 | + long fileSize = entry.getSize(); |
| 344 | + addFilePath(filePaths, entry.getName(), fileSize); |
| 345 | + } |
344 | 346 | } |
345 | | - }); |
| 347 | + } |
346 | 348 | } |
347 | 349 |
|
348 | 350 | /** |
@@ -414,26 +416,26 @@ private String buildXmlResponse(List<String> filePaths) { |
414 | 416 | */ |
415 | 417 | private String extractFile(InputStream inputStream, String fileType) throws Exception { |
416 | 418 | List<String> filePaths = new ArrayList<>(); |
417 | | - Path tempFile = null; |
| 419 | + //Path tempFile = null; |
418 | 420 | FileSystem zipFileSystem = null; |
419 | 421 |
|
420 | 422 | try { |
421 | 423 | // Create a temporary file based on the file type |
422 | | - tempFile = createTempFile(fileType); |
| 424 | + //tempFile = createTempFile(fileType); |
423 | 425 |
|
424 | 426 | // Copy the input stream to the temporary file |
425 | | - Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); |
| 427 | + //Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); |
426 | 428 |
|
427 | 429 | // Process the file based on its type |
428 | 430 | if (ARCHIVE_TYPE_TAR.equals(fileType)) { |
429 | | - processTarFile(filePaths, tempFile); |
| 431 | + processTarFile(filePaths, inputStream); |
430 | 432 | } else { |
431 | | - zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); |
432 | | - processZipFile(filePaths, zipFileSystem); |
| 433 | + //zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); |
| 434 | + processZipFile(filePaths, inputStream); |
433 | 435 | } |
434 | 436 | } finally { |
435 | 437 | closeFileSystem(zipFileSystem); |
436 | | - deleteTempFile(tempFile); |
| 438 | + //deleteTempFile(tempFile); |
437 | 439 | } |
438 | 440 |
|
439 | 441 | return buildXmlResponse(filePaths); |
|
0 commit comments