-1

i have a form which uploads file. my controller :-

public class ConsentController {
@Autowired
private ConsentRepository consentRepository;
private static String UPLOADED_FOLDER = "E://java//files//"; //this directory is used to save the uploaded file
public String filepath;
@RequestMapping(value="/addconsent",headers=("content-type=multipart/*"),method = RequestMethod.POST)
public String addConsent(@RequestParam("consentstatus") String consentStatus,
                         @RequestParam("file")MultipartFile file,
                         @RequestParam("pid")Participants pid,
                         @RequestParam("participantsid") long participantsid,
                         @RequestParam("userid")User userid,
                         @RequestParam("consentid") long consentid
                         ){
  if(consentRepository.findByParticipants(pid)==null){
      if(file.isEmpty()) {
          Consent consent = new Consent(consentStatus,"null",userid,pid);
          consentRepository.save(consent);
      }
      else{
          try {

              // Get the file and save it somewhere
              byte[] bytes = file.getBytes();
              Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
              Files.write(path, bytes);


              //get the path of the saved filed
              String filename = file.getOriginalFilename();
              String filepath = Paths.get(UPLOADED_FOLDER, filename).toString();
              this.filepath=filepath;
              Consent consent=new Consent(consentStatus,this.filepath,userid,pid);
              consentRepository.save(consent);
          }catch (Exception ex){
              System.out.println("Error"+ex.getMessage());
          }
      }
  }

This code perfectly uploads and stores my uploaded file in e drive. but now i want to zip the file before saving it to directory. for now if i upload images.jpg it uploads images.jpg. i want this images.jpg to be saved as (any name) but in zipped format.

sagar limbu
  • 1,202
  • 6
  • 27
  • 52
  • just do it. [example 1](http://www.journaldev.com/957/java-zip-file-folder-example) - [example 2](http://stackoverflow.com/questions/37404541/create-one-zip-file-using-a-set-of-pdf-files) – Patrick Apr 05 '17 at 06:42

2 Answers2

0

Here is a zip demo from oracle article

import java.io.*;
import java.util.zip.*;

public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
  try {
     BufferedInputStream origin = null;
     FileOutputStream dest = new 
       FileOutputStream("c:\\zip\\myfigs.zip");
     ZipOutputStream out = new ZipOutputStream(new 
       BufferedOutputStream(dest));
     //out.setMethod(ZipOutputStream.DEFLATED);
     byte data[] = new byte[BUFFER];
     // get a list of files from current directory
     File f = new File(".");
     String files[] = f.list();

     for (int i=0; i<files.length; i++) {
        System.out.println("Adding: "+files[i]);
        FileInputStream fi = new 
          FileInputStream(files[i]);
        origin = new 
          BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(files[i]);
        out.putNextEntry(entry);
        int count;
        while((count = origin.read(data, 0, 
          BUFFER)) != -1) {
           out.write(data, 0, count);
        }
        origin.close();
     }
     out.close();
  } catch(Exception e) {
     e.printStackTrace();
  }
}
} 
Seamas
  • 1,041
  • 7
  • 13
0

You need to use ZipOutputStream to write data into zip in java. Here is an example:

public static File zip(List<File> files, String filename) {
    File zipfile = new File(filename);
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
        // create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        // compress the files
        for(int i=0; i<files.size(); i++) {
            FileInputStream in = new FileInputStream(files.get(i).getCanonicalName());
            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            // 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 zipfile;
    } catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
    return null;
}

Here is tutorial that shows how to compress and decompress files in java http://www.oracle.com/technetwork/articles/java/compress-1565076.html

Jay Smith
  • 2,331
  • 3
  • 16
  • 27