'File downloads but while extracting it gives "invalid zip file"

I am working on a jsp zip file download ,I am able to download the file but while extracing I get Invalid zip file error. In the java controller I have added

 @RequestMapping("/upload")
public void getDownload(ModelView mv,HttpServletResponse response)
{
        String fileName = "";
            String fullZipFileName = zipDir+ zipFileName;
            
               FileManager fm = FileManager.getInstance();
            
             zipOS = fm.getZipOutputStream(fullZipFileName);            
                    
            try {​​​​​
            
               for(int i = 0; i < numOfFiles; i++) {​​​​​
                  fileName = (String) selectedFiles.get(i);
            
                  File file = new File(directory, fileName);
                  
                  bIS = fm.getBufferedInputStream(file.getAbsolutePath());
                 
                  ZipEntry entry = new ZipEntry(file.getName());
                  zipOS.putNextEntry(entry);
            
                  while((count = bIS.read(fileToAddToZip, 0, buffer)) != -1) {​​​​​
                     zipOS.write(fileToAddToZip, 0, count);
                  }​​​​​
                  bIS.close();
               }​​​​​
               zipOS.close();
        
        FileManager fm = FileManager.getInstance();
        bIS = fm.getBufferedInputStream(zipFileName);
        
        byte[] byteArray = new byte[bIS.available()];
        bIS.read(byteArray);
        bIS.close();
           
    response.setContentType("application/octate-stream");
     response.setHeader("Content-Disposition", "attachment;filename" + zippedFilename);    
    response.getOutputStream().write(byteArray);
}

In the jsp side I have added

    $.ajax({​​​​​​​​​​​
        type: 'POST',
        url: "${​​​​​​​​​​​pageContext.request.contextPath}​​​​​​​​​​​/download",
       
        contentType: "application/json",
        data: JSON.stringify(payload),
        success: function (res, textStatus, response) {​​​​​​​​​​​
            var fileName = response.getResponseHeader("content-disposition").replace("attachment;filename=", "");
var fileName = response.getResponseHeader("Content-Disposition").replace("attachment;filename=", "");

      var bytes = new Uint8Array(res.length);
      for(var i=0;i< res.length;i++)
      {
      var as=res.charCodeAt(i);
      bytes[i]=as;
       }

            var blob = new Blob([bytes], {​​​​​​​​​​​ type: "application/octetstream" }​​​​​​​​​​​);
    
            //Check the Browser type and download the File.
    
            var isIE = false || !!document.documentMode;
    
            if (isIE) {​​​​​​​​​​​
    
                window.navigator.msSaveBlob(blob, fileName);
    
            }​​​​​​​​​​​ else {​​​​​​​​​​​
    
                var url = window.URL || window.webkitURL;
    
                link = url.createObjectURL(blob);
    
                var a = $("<a />");
    
                                    a.attr("download", fileName);
    
                a.attr("href", link);
    
                $("body").append(a);
    
                a[0].click();
    
                $("body").remove(a);
    
            }​​​​​​​​​​​
    
        }​​​​​​​​​​​,
        error: function (e) {​​​​​​​​​​​
           
        }​​​​​​​​​​​
    
    }​​​​​​​​​​​);

I am able to download the file but when I try to extract the zip file ,I get Invalid zip file. Can someone tell me how can I fix this issue



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source