Put Inputstream containing multiple files into one ZipEntry - java

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();
}
}

Related

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.

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.

making zip process more optimized one

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.

Cannot delete zip file after unzipping?

I am facing an unusal issue.I am building a tool which is scheduled to run every 5 mins.
It will pick up the zip files from a particular directory and extract files (depending on the file name) to a destination. I am using zipentry to get each filename in zip file and then extracting as required then I back them (zip files, once I finish all the files in a zip) to a particular directory and then delete the zip file. But sometimes (not always) the zip files do not get deleted. Since I am using fileutils.forcedelete(). I am getting an exception: unable to delete file. So I changed to the code to using fileutils.forcedeleteonexit() still some files remain in the source.
Here is a sample of my code:
sourceFile=new file(zipfile);
zipFile = new ZipFile(sourceFile);
zEnum = (Enumeration<ZipEntry>) zipFile.entries();
for (int a = 0; a < zipFile.size(); a++)
{
ZipEntry zE = zEnum.nextElement();
//Function uses zip4j for extracting. No streams used.
extract(String sourceZipFile, String fileNameToExtract, String outputFolder);
}
//I tried it with finally either
zipFile.close();
//Using fileutils to copy. No streams used.
copyFile(sourceFile, backup);
FileUtils.forceDeleteOnExit(sourceFile);
There are no streams used but I am getting a lock on files sometimes (not always).
What seems to be the bug here? Is it the zip4j extraction that is causing the problem or anything else? I am using zip4j 1.3.1.
I think your problem related with OS file buffers, that sometimes are not flushed when you are trying to delete file.
Did you try to use sourceFile.deleteOnExit() instead FileUtils.forceDeleteOnExit(sourceFile)?
Also you can try to check sourceFile.canWrite before deleting (may be it may helps)
You can also try to use FileInputStream() before deleting:
FileInputStream fi = new FileInputStream(sourceFile);
fi.getFD().sync();
Use apache-commons IO's FileDeleteStrategy. Something like:
FileDeleteStrategy.FORCE.delete(file);
Update:
It should be the way IO is being handled in your application. I have written simple code which copies a zip file to a temporary zip, deflates the temporary zip and after few seconds deletes it. Here you go:
public class ZipTest {
private static String dirPath = "/home/ubuntuuser/Desktop/";
public static void main(String[] args) throws Exception {
File myzip = new File(dirPath + "content.zip");
String tempFileStr = dirPath + "content_temp.zip";
File tempFile = new File(tempFileStr);
String unzipFolderStr = dirPath + "unzip";
copyUsingChannels(myzip, tempFile);
// copyUsingStreams(myzip, tempFile);
unZip(tempFileStr, unzipFolderStr);
Thread.sleep(3000);
tempFile.delete();
}
private static void copyUsingStreams(File myzip, File tempFile)
throws IOException, FileNotFoundException {
byte[] barray = new byte[1024];
if (!tempFile.exists())
{
tempFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(tempFile);
FileInputStream fis = new FileInputStream(myzip);
int length = 0;
while ((length = fis.read(barray)) != -1)
{
fos.write(barray, 0, length);
}
fis.close();
fos.close();
}
public static void copyUsingChannels(final File srcFile, final File destFile) throws Exception
{
if (!destFile.exists())
{
destFile.createNewFile();
}
FileChannel source = new FileInputStream(srcFile).getChannel();
FileChannel destination = new FileOutputStream(destFile).getChannel();
source.transferTo(0, source.size(), destination);
source.close();
destination.close();
}
private static void unZip(String zipFile, String outputFolder) throws Exception {
byte[] buffer = new byte[1024];
File folder = new File(outputFolder);
if (!folder.exists()) {
folder.mkdir();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
System.out.println("file unzip : " + newFile.getAbsoluteFile());
new File(newFile.getParent()).mkdirs();
if (ze.isDirectory())
{
newFile.mkdir();
ze = zis.getNextEntry();
continue;
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
}

How can i zip files in Java and not include files paths

For example, I want to zip a file stored in /Users/me/Desktop/image.jpg
I made this method:
public static Boolean generateZipFile(ArrayList<String> sourcesFilenames, String destinationDir, String zipFilename){
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// VER SI HAY QUE CREAR EL ROOT PATH
boolean result = (new File(destinationDir)).mkdirs();
String zipFullFilename = destinationDir + "/" + zipFilename ;
System.out.println(result);
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFullFilename));
// Compress the files
for (String filename: sourcesFilenames) {
FileInputStream in = new FileInputStream(filename);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filename));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
} // Complete the ZIP file
out.close();
return true;
} catch (IOException e) {
return false;
}
}
But when I extract the file, the unzipped files have the full path.
I don't want the full path of each file in the zip i only want the filename.
How can I made this?
Here:
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filename));
You're creating the entry for that file using the whole path. If you just use the name ( without the path ) you'll have what you need:
// Add ZIP entry to output stream.
File file = new File(filename); //"Users/you/image.jpg"
out.putNextEntry(new ZipEntry(file.getName())); //"image.jpg"
You're finding your source data using the relative path to the file, then setting the Entry to the same thing. Instead you should turn the source into a File object, and then use
putNextEntry(new ZipEntry(sourceFile.getName()))
that'll give you just the final part of the path (ie, the actual file name)
Do as Jason said, or if you want to keep your method signature, do it like this:
out.putNextEntry(new ZipEntry(new File(filename).getName()));
or, using FileNameUtils.getName from apache commons/io:
out.putNextEntry(new ZipEntry(FileNameUtils.getName(filename)));
You could probably get away with accessing source files via new FileInputStream(new File(sourceFilePath, sourceFileName)).
// easy way of zip a file
import java.io.*;
import java.util.zip.*;
public class ZipCreateExample{
public static void main(String[] args) throws Exception {
// input file
FileInputStream in = new FileInputStream("F:/ZipCreateExample.txt");;
// out put file
ZipOutputStream out =new ZipOutputStream(new FileOutputStrea("F:/tmp.zip"));
// name of file in zip folder
out.putNextEntry(new ZipEntry("zippedfile.txt"));
byte[] b = new byte[1024];
int count;
// writing files to new zippedtxt file
while ((count = in.read(b)) > 0) {
System.out.println();
out.write(b, 0, count);
}
out.close();
in.close();
}
}
try {
String zipFile = "/locations/data.zip";
String srcFolder = "/locations";
File folder = new File(srcFolder);
String[] sourceFiles = folder.list();
//create byte buffer
byte[] buffer = new byte[1024];
/*
* To create a zip file, use
*
* ZipOutputStream(OutputStream out) constructor of ZipOutputStream
* class.
*/
//create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(zipFile);
//create object of ZipOutputStream from FileOutputStream
ZipOutputStream zout = new ZipOutputStream(fout);
for (int i = 0; i < sourceFiles.length; i++) {
if (sourceFiles[i].equalsIgnoreCase("file.jpg") || sourceFiles[i].equalsIgnoreCase("file1.jpg")) {
sourceFiles[i] = srcFolder + fs + sourceFiles[i];
System.out.println("Adding " + sourceFiles[i]);
//create object of FileInputStream for source file
FileInputStream fin = new FileInputStream(sourceFiles[i]);
/*
* To begin writing ZipEntry in the zip file, use
*
* void putNextEntry(ZipEntry entry) method of
* ZipOutputStream class.
*
* This method begins writing a new Zip entry to the zip
* file and positions the stream to the start of the entry
* data.
*/
zout.putNextEntry(new ZipEntry(sourceFiles[i].substring(sourceFiles[i].lastIndexOf("/") + 1)));
/*
* After creating entry in the zip file, actually write the
* file.
*/
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
/*
* After writing the file to ZipOutputStream, use
*
* void closeEntry() method of ZipOutputStream class to
* close the current entry and position the stream to write
* the next entry.
*/
zout.closeEntry();
//close the InputStream
fin.close();
}
}
//close the ZipOutputStream
zout.close();
System.out.println("Zip file has been created!");
} catch (IOException ioe) {
System.out.println("IOException :" + ioe);
}
If you zip two files of the same name but with different paths you will run into duplicate file entry errors, though.

Categories