001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonArray; 005import com.eclipsesource.json.JsonObject; 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.OutputStream; 009import java.net.URL; 010import java.util.List; 011 012/** 013 * Provides methods to allow users to download multiple files and folders as a single zip file. Users can download 014 * up to either 32GB or 10,000 files in one batch (whichever limitation is hit first) as a single zip file. 015 */ 016public class BoxZip { 017 /** 018 * Zip URL Template. 019 */ 020 public static final URLTemplate ZIP_URL_TEMPLATE = new URLTemplate("zip_downloads"); 021 /** 022 * Zip Download URL Template. 023 */ 024 public static final URLTemplate ZIP_DOWNLOAD_URL_TEMPLATE = new URLTemplate("zip_downloads/%s/content"); 025 private static final int BUFFER_SIZE = 8192; 026 private final BoxAPIConnection api; 027 028 /** 029 * Constructs a Zip to be used by everything. 030 * 031 * @param api the API connection to be used by the Zip. 032 */ 033 public BoxZip(BoxAPIConnection api) { 034 this.api = api; 035 } 036 037 /** 038 * Creates a zip of multiple files and folders. 039 * 040 * @param name the name of the zip file to be created 041 * @param items list of files or folders to be part of the created zip 042 * @return information about the created zip file 043 */ 044 public BoxZipInfo create(String name, List<BoxZipItem> items) { 045 JsonArray itemsArray = new JsonArray(); 046 for (BoxZipItem item : items) { 047 itemsArray.add(item.getJSONObject()); 048 } 049 JsonObject requestJSON = new JsonObject(); 050 requestJSON.add("items", itemsArray); 051 requestJSON.add("download_file_name", name); 052 053 URL url = ZIP_URL_TEMPLATE.build(this.getAPI().getBaseURL()); 054 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST"); 055 request.setBody(requestJSON.toString()); 056 BoxJSONResponse response = (BoxJSONResponse) request.send(); 057 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 058 059 return new BoxZipInfo(responseJSON); 060 } 061 062 /** 063 * Creates a zip and downloads it to a given OutputStream. 064 * 065 * @param name the name of the zip file to be created 066 * @param items list of files or folders to be part of the created zip 067 * @param output the stream to where the zip file will be written. 068 * @return information about status of the download 069 */ 070 public BoxZipDownloadStatus download(String name, List<BoxZipItem> items, OutputStream output) { 071 return this.download(name, items, output, null); 072 } 073 074 /** 075 * Creates a zip and downloads its contents its to a given OutputStream. 076 * 077 * @param name the name of the zip file to be created 078 * @param items list of files or folders to be part of the created zip 079 * @param output the stream to where the zip file will be written. 080 * @param listener a listener for monitoring the download's progress. 081 * @return information about status of the download 082 */ 083 public BoxZipDownloadStatus download(String name, List<BoxZipItem> items, OutputStream output, 084 ProgressListener listener) { 085 BoxZipInfo zipInfo = this.create(name, items); 086 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), zipInfo.getDownloadURL(), "GET"); 087 BoxAPIResponse response = request.send(); 088 InputStream input = response.getBody(listener); 089 090 byte[] buffer = new byte[BUFFER_SIZE]; 091 try { 092 int n = input.read(buffer); 093 while (n != -1) { 094 output.write(buffer, 0, n); 095 n = input.read(buffer); 096 } 097 } catch (IOException e) { 098 throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e); 099 } finally { 100 response.disconnect(); 101 } 102 BoxAPIRequest statusRequest = new BoxAPIRequest(this.getAPI(), zipInfo.getStatusURL(), "GET"); 103 BoxJSONResponse statusResponse = (BoxJSONResponse) statusRequest.send(); 104 JsonObject statusResponseJSON = Json.parse(statusResponse.getJSON()).asObject(); 105 return new BoxZipDownloadStatus(statusResponseJSON); 106 } 107 108 /** 109 * Gets the API connection used by this resource. 110 * 111 * @return the API connection used by this resource. 112 */ 113 public BoxAPIConnection getAPI() { 114 return this.api; 115 } 116}