Writing a zip file on Google App engine

package com.lin.web.servlet;


import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.lin.dfp.api.DFPReportService;
import com.lin.dfp.api.IDFPReportService;
import com.lin.web.gaebigquery.BigQueryUtil;
import com.lin.web.util.LinMobileConstants;
import com.lin.web.util.LinMobileVariables;


public class GAEZipFileDownloader extends HttpServlet {

  private static final long serialVersionUID = 1L;

  private static final String GCS_URI = "http://commondatastorage.googleapis.com";
  static final String UPLOAD_URL="https://www.googleapis.com/upload/storage/v1beta2/b/linmobile_qa/o?uploadType=multipart";

  /** Global configuration of Google Cloud Storage OAuth 2.0 scope. */
  private static final String STORAGE_SCOPE =
      "https://www.googleapis.com/auth/devstorage.read_write";


  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();
  private static final Logger log = Logger.getLogger(BigQueryUtil.class.getName());
  private static List<String> SCOPES=new ArrayList<>();
 
  private static GoogleCredential credential;
  
  static final int BUFFER_SIZE = 2* 1024 * 1024;
 
 
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
      System.out.println("here........");
      ByteArrayOutputStream zipBaos = new ByteArrayOutputStream();
    try {
        String fileURL="http://storage.googleapis.com/myBucket/test.csv.gz";
        IDFPReportService dfpReportService=new DFPReportService();
        String csvData=dfpReportService.readCSVGZFile(fileURL);
        StorageServiceServlet st=new StorageServiceServlet();
        System.out.println("csv data:"+csvData);
      
        InputStream in = new ByteArrayInputStream(csvData.getBytes());
      
     
        ZipOutputStream zipOut = new ZipOutputStream(zipBaos);
      
        byte[] file1Contents = IOUtils.toByteArray(in);
      
        ZipEntry zipEntry1 = new ZipEntry("testFile.csv");     // Create new zip entry                                                                     // the constructor parameter is filename of the entry
        zipOut.putNextEntry(zipEntry1);            // add the zip entry to the ZipOutputStream
        zipOut.write(file1Contents);               // write contents (byte array) to the ZipOutputStream
        zipOut.closeEntry();  
        zipOut.close();
      
     
      
      /*  String csPath=st.uploadViaZip(csvData, "test_proc.csv.gz", "test", LinMobileVariables.APPLICATION_BUCKET_NAME);
        System.out.println("csPath:"+csPath); */ 
        System.out.println("Done............");
      
      } catch (Throwable e) {
          e.printStackTrace();
          // Creation of the zip file failed; inform the browser about it
          res.sendError(500, "Creation of the zip file failed with exception:\n\n" + e.getLocalizedMessage());
         return;
    }
  
    // Get the zip data from the ByteArrayOutputStream
    byte[] zipData = zipBaos.toByteArray();


    // Serve the data to response's stream
    String filename = "MyZipFile.zip";

    // following header statement instructs the web browser
    // to treat the file as attachment (= to download the file)
    res.setHeader("Content-Disposition", "attachment; filename=" + filename);

    res.setContentType("application/x-download");
    res.setContentLength(zipData.length);
    res.getOutputStream().write(zipData);  
  }
 
  
 
}

Comments

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

How to create mail message in FRC822 format