Are there any ZIP libraries available in Java that can handle real files without creating them?

Asked 2 years ago, Updated 2 years ago, 99 views

I wrote the code with the spring boot.

A feature that allows you to take several directories from a resource, compress them into a zip, and download them.

It is currently developed using the zip4j library.

File.createTempFile("test_", ".zip");
String tempFilePath = createTempFile.getAbsolutePath();
createTempFile.delete(); // create and delete to take only different temporary file paths in different OSs
ZipFile zipFile = new ZipFile(createTempFile);
... a summary of...
FileInputStream fileInputStream = new FileInputStream(zipFile.getFile());
byte[] binaryData = IOUtils.toByteArray(fileInputStream);
fileInputStream.close();
new File(tempFilePath).delete();

Create a zip file as above (to be exact, create a temporary file in the tmp path that fits each OS, take the temporary file path, delete it, and leave it to zip4j) and send the zip file down using the addStream method. The zip file has been deleted since then.

I guess when you create a ZipFile object in zip4j, the constructor actually creates the file internally. Because if you do not call the createTempFile.delete(); method, an excursion occurs.

Although the zip4j library is very good in terms of ease of use, it bothers me to create temporary files and delete them after I get the byte array. Instead of creating a zip file as a real file, I want to send it to Response Entities by byte array with only streams.

I've searched github, but I don't think I found a library that fits my needs exactly my needs.

Please let me know if you know any open source libraries.

java spring spring-boot

2022-09-20 21:36

1 Answers

The window on the screen is Zeppelin.Create a sample with a scalar.

The sample code is not much different from Java, so I think you will understand it easily.

The code is to compress the string ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC.

import java.util.zip._
import java.io._

val buffer = new ByteArrayOutputStream(4096)
val w = new ZipOutputStream(buffer)
w.putNextEntry(new ZipEntry("filename.txt"))
w.write(Array[Byte](65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67, 65, 66, 67))
w.finish()
w.flush()
w.closeEntry()


2022-09-20 21:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.