Skip to content

Commit 0bdb3b9

Browse files
committed
removed creating of tempFile from inputstream, used imputstream directly
1 parent 5ecfa18 commit 0bdb3b9

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

dspace-api/src/main/java/org/dspace/content/PreviewContentServiceImpl.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
import java.io.UnsupportedEncodingException;
1515
import java.nio.charset.StandardCharsets;
1616
import java.nio.file.FileSystem;
17-
import java.nio.file.FileSystems;
1817
import java.nio.file.Files;
1918
import java.nio.file.Path;
2019
import java.nio.file.Paths;
21-
import java.nio.file.StandardCopyOption;
2220
import java.sql.SQLException;
2321
import java.util.ArrayList;
2422
import java.util.Arrays;
@@ -31,6 +29,8 @@
3129
import java.util.function.Function;
3230
import java.util.regex.Matcher;
3331
import java.util.regex.Pattern;
32+
import java.util.zip.ZipEntry;
33+
import java.util.zip.ZipInputStream;
3434

3535
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
3636
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
@@ -311,38 +311,40 @@ private void addFilePath(List<String> filePaths, String path, long size) {
311311
filePaths.add(fileInfo);
312312
}
313313

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)) {
323322
TarArchiveEntry entry;
324323
while ((entry = tis.getNextTarEntry()) != null) {
324+
// Add the file path and its size (from the TAR entry)
325325
addFilePath(filePaths, entry.getName(), entry.getSize());
326326
}
327327
}
328328
}
329329

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+
}
344346
}
345-
});
347+
}
346348
}
347349

348350
/**
@@ -414,26 +416,26 @@ private String buildXmlResponse(List<String> filePaths) {
414416
*/
415417
private String extractFile(InputStream inputStream, String fileType) throws Exception {
416418
List<String> filePaths = new ArrayList<>();
417-
Path tempFile = null;
419+
//Path tempFile = null;
418420
FileSystem zipFileSystem = null;
419421

420422
try {
421423
// Create a temporary file based on the file type
422-
tempFile = createTempFile(fileType);
424+
//tempFile = createTempFile(fileType);
423425

424426
// Copy the input stream to the temporary file
425-
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
427+
//Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
426428

427429
// Process the file based on its type
428430
if (ARCHIVE_TYPE_TAR.equals(fileType)) {
429-
processTarFile(filePaths, tempFile);
431+
processTarFile(filePaths, inputStream);
430432
} else {
431-
zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null);
432-
processZipFile(filePaths, zipFileSystem);
433+
//zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null);
434+
processZipFile(filePaths, inputStream);
433435
}
434436
} finally {
435437
closeFileSystem(zipFileSystem);
436-
deleteTempFile(tempFile);
438+
//deleteTempFile(tempFile);
437439
}
438440

439441
return buildXmlResponse(filePaths);

0 commit comments

Comments
 (0)