making zip process more optimized one - java

I have developed a method to zip a file that will take file path and filename as a parameter and will zip a file as shown below could you please advise how can I modify this method to be more efficient and more fast as I am a big fan of optimization..
public File generateZipForAFile(String folderPath, String reportFileName)
throws FileNotFoundException, IOException {
File inputFile = new File(folderPath + reportFileName);
FileInputStream in = new FileInputStream(inputFile);
File outputZipFile = new File(folderPath, reportFileName + ".zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(reportFileName ));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
out.close();
in.close();
return outputZipFile;
}

There is not much you can do in your code. Furthermore, most of the time is spent while actually doing the zip.
So even if you reduce the time spent in your part to 0 the overall gain will be very small.

Related

Unable to compress a CSV file Java

I'm trying to convert an existing CSV file to a gzip file.
I verified the CSV looks good. Once I run this code, I get a "failed to expand" error and tried an online decompression tool that also failed, so it seems the output zip is corrupt.
public void compressGzip(String input, String dest) throws IOException {
Path pathSource = Paths.get(input);
Path destSource = Paths.get(dest);
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(destSource.toFile()));
FileInputStream fis = new FileInputStream(pathSource.toFile())) {
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
gos.write(buffer, 0, len);
}
}
}
Anything I could be missing here?

Zip multipartfile and store via FTP

I take a multipartfile (i.e. SAMPLE.csv) in input.
I should zip it (i.e. SAMPLE.zip) and store it via FTP.
public void zipAndStore(MultipartFile file) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
InputStream is = file.getInputStream()) {
ZipEntry zipEntry = new ZipEntry("SAMPLE.zip");
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
storeFtp("SAMPLE.zip", new ByteArrayInputStream(baos.toByteArray()));
} catch (Exception e) {
}
}
The storeFtp use the org.apache.commons.net.ftp.FTPClient.storeFile(String remote, InputStream local) method.
The problem is that the uploaded file is corrupted and i'm unable to manually decompress.
What's wrong?
A zipfile has a list of DirEntries and a endLocator at the end of the file (after all the ZipFileRecords, i.e. the ZipEntries in the code).
So you probably have to close the zipfile before calling storeFtp() to make sure the DirEntries and the endLocator are written to the file:
zos.closeEntry();
zos.close();
storeFtp("SAMPLE.zip", new ByteArrayInputStream(baos.toByteArray()));
(I don't know Java that well, so I can't check or test the full code)
Also check out this answer.

Put Inputstream containing multiple files into one ZipEntry

I want to zip an array of File to a zipfile and send it to the browser. The Inputstream of each File is a shapefile, and actually consists of multiple files (.shp, .dbf, .shx, ...).
When I send only one File with the following code, it works properly and a zipfile is returned with all the desired files in it.
Code to send a single file
FileInputStream is = new FileInputStream(files.get(0));
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + getCurrentUser(request).getNiscode() + ".zip");
while (is.available() > 0) {
response.getOutputStream().write(is.read());
}
is.close();
if (response.getOutputStream() != null) {
response.getOutputStream().flush();
response.getOutputStream().close();
}
When I try to send all the files together, a zipfile is returned with the desired folders, but in each folder only one element with just a .file extension is present. It has something to do with the entries of the ZipOutputStream?
Code to send all the files
byte[] zip = this.zipFiles(files, Ids);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename="test.zip");
response.getOutputStream().write(zip);
response.flushBuffer();
private byte[] zipFiles(ArrayList<File> files, String[] Ids) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
int count = 0;
for (File file : files) {
FileInputStream fis = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(Ids[count] + "/"));
zos.putNextEntry(new ZipEntry(Ids[count] + "/" + file.getName()));
while (fis.available() > 0) {
zos.write(fis.read());
}
zos.closeEntry();
fis.close();
count ++;
}
zos.flush();
baos.flush();
zos.close();
baos.close();
return baos.toByteArray();
}
Based on your code, it seems like every file inside your files array is already a zip file
When you then later do zipFiles, you are making a zipfile that contains more zipfiles in its folders. You obviously don't want this, but you want a zipfile that has folders that contain the contents of all could zipfiles.
Basing on an existing an existing answer of Thanador located at "Reading data from multiple zip files and combining them to one" I devised the following solution to also include directories and proper stream handling:
/**
* Combine multiple zipfiles together
* #param files List of file objects pointing to zipfiles
* #param ids List of file names to use in the final output
* #return The byte[] object representing the final output
* #throws IOException When there was a problem reading a zipfile
* #throws NullPointerException When there either input is or contains a null value
*/
private byte[] zipFiles(ArrayList<File> files, String[] ids) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Buffer to copy multiple bytes at once, this is generally faster that just reading and writing 1 byte at a time like your previous code does
byte[] buf = new byte[16 * 1024];
int length = files.size();
assert length == ids.length;
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
for (int i = 0; i < length; i++) {
try (ZipInputStream inStream = new ZipInputStream(new FileInputStream(files.get(i))) {
ZipEntry entry;
while ((entry = inStream.getNextEntry()) != null) {
zos.putNextEntry(new ZipEntry(ids[i] + "/" + entry.getName()));
int readLength;
while ((readLength = inStream.read(buf)) > 0) {
zos.write(buf, 0, readLength);
}
}
}
}
}
return baos.toByteArray();
}
Technically, its faster and more memory efficient to directly write to the output stream you got from response.getOutputStream(), but I didn't do this in the above example, so you would have an easier time implementing the method in your code
If you close a stream, it automatically flushes it, I'm using try-with-resources to close them
try following solution:
private byte[] zipFiles(ArrayList<File> files, String[] Ids) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
for (File file : files) {
ZipEntry ze= new ZipEntry(file.getName());
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream(file);
int len;
while ((len = fis .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
fis .close();
}
byte[] byteArray = baos.toByteArray();
zos.flush();
baos.flush();
zos.close();
baos.close();
return byteArray;
}
IDK why you put count variable and why you put twice zos.putNextEntry(new ZipEntry()).
public static void compressListFiles(List<Pair<String, String>> filePairList, ZipOutputStream zipOut) throws Exception {
for (Pair<String, String> pair : filePairList) {
File fileToZip = new File(pair.getValue());
String fileId = pair.getKey();
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileId + "/" + fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
}

How to directly download a file from server,without saving it in any folder?

if(!ErmUtil.isNull(listOfActualFilePaths) && listOfActualFilePaths.size()>0){
FileOutputStream fos = new FileOutputStream("/smiles/wrk/attachments/ermWeb/taxation/testing.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
Iterator itrOnFNames = listOfActualFilePaths.iterator();
while (itrOnFNames.hasNext()) {
StringBuffer ActualPath = (StringBuffer) itrOnFNames.next();
addToZipFile(ActualPath.toString(), zos);
}
zos.close();//Closing Both Streams
fos.close();
}
public void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
int index = fileName.lastIndexOf("/");
String fileNameForZip = fileName.substring(index+1);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileNameForZip);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
I am using above code, to save zip-ed file on a specific location. But what i want to do is that, instead of saving that file, it get downloaded directly.
Edit 1
See If I want to download a zip file,then according to above code, on path /smiles/wrk/attachments/ermWeb/taxation/testing.zip it will be saved first,then from that folder, I(server) can send it to client(Computer).
But I don't want to save it on the specified path,Instead of "saving first to folder and then sending to client", I directly want to send it to client.

Whats wrong with this zip method?

I have a method which zips up 5 files. It produces a zip file without error, but I cannot open it to examine the contents. I tried emailing it and gmail said it cannot send corrupt files. Trying to open with WinRAR in Windows results in an error stating:
The archive is either in unknown format or damaged
This is the method:
private void zipTestFiles() throws FileNotFoundException, IOException
{
File[] filenames = fileDir.listFiles(fileNameFilter(Constants.PAGE_MON_FILENAME_FILTER));
byte[] buf = new byte[1024];
String outFilename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + Constants.PAGEMONITOR_ZIP;
DeflaterOutputStream out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(outFilename)));
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
out.close();
}
You should use ZipOutputStream instead of DeflaterOutputStream. And do not forget to create entries. Read javadoc of ZipOutputStream before writing the implementation.
Try with ZipOutputStream which already exists in Java. DeflaterOutputStream only uses DEFLATE method to compress but doesn't put ZIP headers automatically.

Categories