Zip file created on server and download that zip, using java
Asked Answered
R

2

6

I have the below code got from mkyong, to zip files on local. But, my requirement is to zip files on server and need to download that. Could any one help.

code wrote to zipFiles:

public void zipFiles(File contentFile, File navFile)
{
    byte[] buffer = new byte[1024];

    try{
        // i dont have idea on what to give here in fileoutputstream
        FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze= new ZipEntry(contentFile.toString());
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(contentFile.toString());

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

        in.close();
        zos.closeEntry();

        //remember close it
        zos.close();

        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace();
    }
}

what could i provide in fileoutputstream here? contentfile and navigationfile are files i created from code.

Rudiment answered 14/5, 2014 at 4:39 Comment(2)
Please clarify your question. Is this code intended to run on the server or the client? If the server, what kind of server? Servlet for example?Muezzin
Implement a server to send data, and a client to recv data? Or use current popular protocols, like ssh and ftp and so on.Evangelin
G
12

If your server is a servlet container, just write an HttpServlet which does the zipping and serving the file.

You can pass the output stream of the servlet response to the constructor of ZipOutputStream and the zip file will be sent as the servlet response:

ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

Don't forget to set the response mime type before zipping, e.g.:

response.setContentType("application/zip");

The whole picture:

public class DownloadServlet extends HttpServlet {

    @Override
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=data.zip");

        // You might also wanna disable caching the response
        // here by setting other headers...

        try ( ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()) ) {
            // Add zip entries you want to include in the zip file
        }
    }
}
Griffis answered 14/5, 2014 at 4:50 Comment(0)
M
0

Try this:

@RequestMapping(value="download", method=RequestMethod.GET)
public void getDownload(HttpServletResponse response) {

    // Get your file stream from wherever.
    InputStream myStream = someClass.returnFile();

    // Set the content type and attachment header.
    response.addHeader("Content-disposition", "attachment;filename=myfilename.txt");
    response.setContentType("txt/plain");

    // Copy the stream to the response's output stream.
    IOUtils.copy(myStream, response.getOutputStream());
    response.flushBuffer();
}

Reference

Maisonette answered 10/11, 2016 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.