Download zip file when call api - java

FileOutputStream fos = new FileOutputStream(FILE_NAME);
ZipOutputStream zipOut = new ZipOutputStream(fos);
for (File file : files) {
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
zipOut.close();
fos.close();
I have a zip file, I want to download it when I call the api.With the zip file, I don't know how to download. Hope to get help from you.

If you are using Spring/Spring Boot. You can follow this stackoverflow thread Download File in Spring

Related

ZipEntry changes UpperCase filename to CamelCase

I am trying to zip up 3 files in a windows environment just before SCP'ing them to a Linux server.
The 3 filenames are all uppercase.
When I add the ZipEntry to the ZipOutputStream, it changes the file name to camel case. This will not work when sending them to Linux.
Is there any way to keep the uppercase names when I use ZipEntry.putNextEntry()?
byte[] buffer = new byte[1024];
String strOutFile = "MyZip.zip";
FileOutputStream fos = new FileOutputStream(strOutFile);
ZipOutputStream zos = new ZipOutputStream(fos);
String strInFile = "FILE1.TXT";
ZipEntry zipEntry = new ZipEntry(strInFile);
zos.putNextEntry(zipEntry);
FileInputStream fis = new FileInputStream(new File(strInFile));
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

Saving ZipOutputStream to zip file with Java

I have to create ZIP archive in memory
But now, I need it to be saved in a real .zip file in a disk. How to do it?
Pseudocode:
public byte[] crtZipByteArray(ByteArrayInputStream data,ZipEntry entry) throws IOException{
ByteArrayOutputStream zipout = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(zipout);
byte[] buffer = new byte[1024];
int len;
zos.putNextEntry(entry);
while ((len = data.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
zos.close();
data.close();
return zipout.toByteArray();
}
Replace ByteArrayOutputStream zipout with FileOutputStream zipout.
If you sill need to return byte array as method result use apache commons TeeOutputStream to duplicate output for two streams.
public byte[] crtZipByteArray(ByteArrayInputStream data,ZipEntry entry) throws IOException{
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
OutputStream fileOut = new FileOutputStream("filename.zip");
OutputStream teeOut = new TeeOutputStream(byteArrayOut, fileOut);
ZipOutputStream zos = new ZipOutputStream(teeOut);
.....
}

Zipping files with ZipOutputStream gives inconsistent results

I want to zip a text file using the java.util.ZipOutputStream class. I found two examples on the internet explaining on how to do that. This led me to the two possible implementations shown below. While both methods produce 'healthy zip files', my problem is that on every run the binary content of the file is slightly different (around the 10th byte). Does someone know if
This is intended behaviour
There is a way to always produce exactly the same result
Here is my current code:
public byte[] getZipByteArray(String fileName) throws IOException
{
byte[] result = new byte[0];
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry ze = new ZipEntry(fileName);
zos.putNextEntry(ze);
InputStream inputStream = ZipCompression.class.getResourceAsStream(fileName);
int len;
while ((len = inputStream.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
zos.closeEntry();
zos.close();
result = baos.toByteArray();
return result;
}
public byte[] ZipByteArrayBuffered(String fileName) throws IOException
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
File file = new File(fileName);
InputStream fileInputStream = ZipCompression.class.getResourceAsStream(file.getName());
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
IOUtils.copy(fileInputStream, zipOutputStream);
fileInputStream.close();
zipOutputStream.closeEntry();
if (zipOutputStream != null)
{
zipOutputStream.finish();
zipOutputStream.flush();
IOUtils.closeQuietly(zipOutputStream);
}
IOUtils.closeQuietly(bufferedOutputStream);
IOUtils.closeQuietly(byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
Byte 10 starts the file modification date and so this will always differ. See Wikipedia for the details of the zip file format.

How to compress and download a xlsx file using zip in java

I'm creating a xlsx using poi and saving it on fileSystem. I need to download the file on a servlet call and due to memory constraints I did not create a xssf workbook object and used the following code instead :
byte[] buf = new byte[1024];
ServletOutputStream sOut = response.getOutputStream();
FileInputStream input = null;
try {
long length = fileToRead.length();
input = new FileInputStream(fileToRead);
while ((input != null) && ((length = input.read(buf)) != -1)) {
sOut.write(buf, 0, (int) length);
}
Where fileToRead is the file present at the file system.
How can I integrate this with How to create a zip file in Java
You could use
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
....
ze = new ZipEntry("xlsData");
zos.putEntry (ze);
// loop
zos.write(buf, 0, (int) length);
// finally
zos.close();

How To : Write a File with FileOutputStream?

What I'm doing is that the file and each line of data I'm reading for BufferOutputStream will be saved in a FileOutputStream and I want to specify the output data.
If i have understood correct, you want to download the file from S3 and write to your local directory using BufferedOutputStream .
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
InputStream is = object.getObjectContent();
// Creating file
File file= new File(localFilePath);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos= new BufferedOutputStream(fos);
int read = -1;
while ((read = is.read()) != -1) {
bos.write(read);
}
bos.flush();
bos.close();
is.close();

Categories

Resources