zipping binary data in java - java

I am trying to zip a group of binary data (result set returned from database) into a single file. Which can be downloaded via web application. Following code is used to zip the result set and write the zip file to HttpServletResponse
String outFilename = "outfile.zip";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename= " + outFilename);
OutputStream os = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(os);
for (int i = 0; i < cardFileList.size(); i++) {
CardFile cardFile = cardFileList.get(i);
out.putNextEntry(new ZipEntry(cardFile.getBinaryFileName()));
out.write(cardFile.getBinaryFile(), 0, cardFile.getBinaryFile().length);
out.closeEntry();
}
// Complete the ZIP file
out.flush();
out.close();
os.close();
The problem is that while unzipping the downloaded zip file using WinRar I get following error :
File Path: Either multipart or corrupt ZIP archive
Can someone point out where am I making mistake?. Any help would be appreciated.
[EDIT] I tried response.setContentType("application/zip"); but same result.

The following code works for me:
FileOutputStream os = new FileOutputStream("out.zip");
ZipOutputStream zos = new ZipOutputStream(os);
try
{
for (int i = 1; i <= 5; i++)
{
ZipEntry curEntry = new ZipEntry("file" + i + ".txt");
zos.putNextEntry(curEntry);
zos.write(("Good morning " + i).getBytes("UTF-8"));
}
}
finally
{
zos.close();
}
Zip files generated with this code opens up with 7-zip without problem.
Check that the response from the servlet is not actually a 404 or 500 error page. Pick a small response and open it up with a hex editor or even a text editor. Zip files start with a 'PK' magic number which should be visible even in a text editor.
Try saving to file instead of servlet output stream for starters and see if that makes a difference.
Could there be a filter modifying your servlet's output / corrupting the ZIP?

Your code looks correct for producing the file. So the problem is probably in your downloading.
look at the file you have downloaded. What is it's size?
try a tool other than your WinRar and see what you have. 7Zip is decent.

Related

File has been moved, can not be read again (Spring mvc)

I am using spring MVC where through API I am uploading zip file using MultipartFile. In backend I have to convert uploaded zip file into InputStream for further processing. But my code is giving error intermittently " File has been moved, can not be read again ".
here is the code snippet :
File temp = null;
InputStream stream = null;
try {
InputStream initialStream = inputFile.getInputStream();
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
temp = File.createTempFile("upload", null);
try (OutputStream outStream = new FileOutputStream(temp)) {
outStream.write(buffer);
}
ZipFile zipFile = new ZipFile(temp);
stream = zipFile.getInputStream(zipFile.getEntries().nextElement());
} catch (Exception e) {
log.error("Exception occurred while processing zip file " + e.getMessage());
throw e;
} finally {
if (temp != null)
temp.delete();
}
return stream;
Here inputFile is MultipartFile.
Could you please suggest what is wrong here?
Your code is returning an input stream from a file that you have deleted - last line is temp.delete().
ZipInputStream has a small internal buffer for decoding, so that may explain why some read calls work after the delete, but it will not be possible to continue reading from a file that you deleted, hence the exception.
Also, the call initialStream.available() is unlikely to be the correct way to determine the size of the input stream file part. Try printing the size / check how to read the actual length of the file in the multipart stream - such as part.getSize(), or transfer the bytes into a new ByteArrayOutputStream() before assigning to buffer.
I would not recommend doing any work with files or multipart streams using direct transfer to byte[] as you risk OutOfMemoryException. However in your case where you are happy to have byte[] for the ZIP and you read the first entry of the ZIP file (and are ignoring other entries) then you could try extracting the first entry as InputStream without writing to a file as follows:
// Read a zip input stream from a zip stored in byte[]:
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(buffer));
// Select first entry from ZIP
ZipEntry entry = zis.getNextEntry();
// You should be able to read the entry from zis directly,
// if this is text file you could test with:
// zis.transferTo(System.out);
return zis;
You should ensure that you close the stream after use.
Potential issues I can see in your code:
temp file is used as zip file, yet you delete the temp file prior to
returning. How can you use the zip file as file stream if you have
deleted it?
Do you support concurrent uploads? If yes, then you have concurrent
resource access problem. Multiple calls to create temp file:
"upload" and process it. Why don't you create a different
filename e.g. with datetime suffix + random number suffix.

Corrupted zip file using ZipOutputStream

I'm trying to create a zip file to be able to send multiple files over http.
My issue is that the Zip file that is generated is "corrupted" before and after the file has been send. The issue is i'm not able to find what i did wrong as i'm getting no errors inside the console.
So does someone has an idea file my generated zip file is corrupted ?
This is my code :
OutputStream responseBody = t.getResponseBody();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
int counter = 1;
for (PDDocument doc : documents)
{
ZipEntry zipEntry = new ZipEntry("document" + counter);
zos.putNextEntry(zipEntry);
ByteArrayOutputStream docOs = new ByteArrayOutputStream();
doc.save(docOs);
docOs.close();
zos.write(docOs.toByteArray());
zos.closeEntry();
zos.finish();
zos.flush();
counter++;
}
zos.close();
baos.close();
responseBody.write(baos.toByteArray());
responseBody.flush();
Thank you for your help !
You need to remove zos.finish() from inside the loop as it terminates the ZIP entries, as it is handled by zos.close() at end of the stream.
With very large streams you will be better off sending ZIP directly to responseBody bypassing ByteArrayOutputStream memory buffer.
If you are still having problems check the content type of the output is set. It might be easier to debug by temporarily writing the byte[] to file to check the ZIP format you are sending with:
Files.write(Path.of("temp.zip"), baos.toByteArray());
This outline below shows sending a simple ZIP over http (from a servlet, adjust the first 2 lines to appropriate calls for "t"). This may help you check which step of your code causes the corruption if you work back to adding your own document objects inside the loop:
// MUST set response content type:
// resp.setContentType("application/zip");
OutputStream out = resp.getOutputStream(); // or t.getResponseBody();
try(ZipOutputStream zos = new ZipOutputStream(out))
{
while (counter-- > 0)
{
ZipEntry zipEntry = new ZipEntry("document" + counter+".txt");
zos.putNextEntry(zipEntry);
zos.write(("This is ZipEntry: "+zipEntry.getName()+"\r\n").getBytes());
}
}

how to flush two files from HTTPServeletResponse simultaneously

I am in a situation where on calling an API i want to download a excel file along with a zip file.
I have cracked the code for downloading them separately, but when put together only one file gets downloaded and the other one just doesnt gets downloaded.
I guss the problem is I cannot use response.getOutPutStream().flush() or response.flushBuffer() simultaneously.
String absolutePath = context.getRealPath("resources/ZipFolders");
String inputFile = Paths.get(absolutePath + "/Attachments.zip").toAbsolutePath().toString();
File finalFile = new File(inputFile);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(finalFile));
String absolutePath2 = context.getRealPath("resources/Spreadsheets");
String inputFile2 = Paths.get(absolutePath2 + "/Validation_Spreadsheet.xlsx").toAbsolutePath().toString();
File file = new File(inputFile2);
byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
ZipEntry zipEntry = new ZipEntry("Validation_Spreadsheet.xlsx");
zipOut.putNextEntry(zipEntry);
zipOut.write(bytes);
zipOut.closeEntry();
zipOut.close();
response.setContentType("application/zip");
response.setHeader("Content-disposition", "attachment;filename=attachment_trial.zip");
response.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(finalFile)));
System.err.println("above flush>>>>>>>>>>>>>>");
response.getOutputStream().flush();
responsetrial.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
responsetrial.setHeader("Content-disposition", "attachment; filename=TransactionErrors.xlsx");
responsetrial.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(file)));
System.err.println("above flush2>>>>>>>>>>>>>>");
responsetrial.getOutputStream().flush();
It's not possible to download 2 files over a single HTTP request.
You will need to make 2 separately requests for this task.
If you need to download many files in a single "HTML button", you need to write some javascript logic two make this.

Improper zip entries when writing filename containing non-English characters even with Java 7

I am trying to develop code that can handle zipping files with non-English characters (Umlaut, Arabic etc) but the zipped file contains improper names. I am using java version 1.7.0_45 thus it shouldn't be due to the bug mentioned here.I am setting the charset to UTF-8 for the ZipOutputStream constructor and as per Javadocs it should work as per my requirements.
I am assured that the zip file is being written correctly as an attempt to read entries from the file gives proper filenames (as expected).
However, when I try to open/extract with either Ubuntu default ArchiveManager/Unzip tool, the filenames are messed up.
Here is my code :
private void convertFilesToZip(List<File> files) {
FileInputStream inputStream = null;
try {
byte[] buffer = new byte[1024];
FileOutputStream fileOutputStream = new FileOutputStream("zipFile.zip");
ZipOutputStream outputStream = new ZipOutputStream(fileOutputStream, Charset.forName("UTF-8"));
for (File file : files) {
inputStream = new FileInputStream(file);
String filename = file.getName();
System.out.println("Adding file : " + filename);
outputStream.putNextEntry(new ZipEntry(filename));
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.closeEntry();
}
if(inputStream != null) inputStream.close();
outputStream.close();
System.out.println("Zip created successfully");
System.out.println("=======================================================");
System.out.println("Reading zip Entries");
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File("zipFile.zip")), Charset.forName("UTF-8"));
ZipEntry zipEntry;
while((zipEntry=zipInputStream.getNextEntry())!=null){
System.out.println(zipEntry.getName());
zipInputStream.closeEntry();
}
zipInputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
the output for the files is as follows:
Adding file : umlaut_ḧ.txt
Adding file : ذ ر ز س ش ص ض.txt
Adding file : äǟc̈ḧös̈ ẗǚẍŸ_uploadFile4.txt
Adding file : pingüino.txt
Adding file : ÄÖÜäöüß- Español deEspaña.ppt
Zip created successfully
=======================================================
Reading zip Entries
umlaut_ḧ.txt
ذ ر ز س ش ص ض.txt
äǟc̈ḧös̈ ẗǚẍŸ_uploadFile4.txt
pingüino.txt
ÄÖÜäöüß- Español deEspaña.ppt
Has anyone successfully implemented what I wish to achieve here.
Can someone point me to what I may have missed or have been doing wrong.I did all the google I could and even tried Apache Commons Compress but still no luck.
It's mentioned in the bug report that the bug is resolved in Java 7, then why is the code not working.
[Update]
I finally figured out that the problem is not in the code but is in fact with the default ArchiveManager of Ubuntu. It doesn't recognizes/extracts the contents properly. When the same file is opened/extracted by the windows zip handler, it works flawlessly.
Additionally, the commons-compress supports a bunch of other formats too apart from the zip,gzip supported by Java.
http://commons.apache.org/proper/commons-compress/index.html

Java : download file outside server context

I need to save a file and download file in directory outside server context.
I am using Apache Tomacat
I am able to do this in directory present in webapps directory of application
If my directory structure is as follows,
--src
--WebContent
-- uploaddir
-- myfile.txt
Then I am able to download in by simply.
download
But, problem is when file is in some other directory say d:\\uploadedfile\\myfile.txt
then I wont be able to download it, as resource is not in server context as above.
I have file path to uuid mapping,
like,
d:\\uploadedfiles\\myfile.txt <-> some_uuid
then I want file should be downloaded, on click of following,
download
So, How to make file downloadable when it is outside the server context,
I heard about getResourceAsStream() method which would do this , But would any one help me on how to do this, probably with simple code snippet?
Try the below code which you can write in filedownloadservet. Fetch the file name from the request parameter and then read and write the file.
If you need to do some security checks then do that before processing the request.
File file = new File("/home/files", "file name which user wants to download");
response.setContentType(getServletContext().getMimeType(file.getName()));
response.setContentLength(file.length());
BufferedInputStream inputStream = null;
BufferedOutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
outputStream = new BufferedOutputStream(response.getOutputStream());
byte[] buf = new byte[2048];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
//log it
}
}
// do the same for input stream also
}
here i found the answer,
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment;filename=downloadname.doc");
File file=new File("d:\\test.doc");
InputStream is=new FileInputStream(file);
int read=0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while((read = is.read(bytes))!= -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
Base path will not work that is for HTML and it works if the base path is also exposed by your web server which does not look like case here.
To download an arbitary file you need to open the file using a FileInputStream (and surround it by a buffered input stream), read a byte, then send that byte from your servlet to the client.
Then there are security concerns, so should google that (basically not give access to any file but only file that is to be shared, audit download etc as needed.
Again in your servlet set the mime type etc and then open a input stream and write the bytes to the output stream to client

Categories