Created
August 14, 2013 16:52
-
-
Save phaniram/6233013 to your computer and use it in GitHub Desktop.
Simple java class downloading a S3 .zip file and extract to local directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ufg; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.PrintStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Enumeration; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
public class UFG { | |
List<String> fileList; | |
public String ZIP_FILE_URL; | |
public String INPUT_ZIP_FILE; | |
public String OUTPUT_FOLDER; | |
public static void copyInputStream(InputStream in, OutputStream out) | |
throws IOException { | |
byte[] buffer = new byte[1024]; | |
int len; | |
while ((len = in.read(buffer)) >= 0) { | |
out.write(buffer, 0, len); | |
} | |
in.close(); | |
out.close(); | |
} | |
public void downloadFile() { | |
try { | |
long startTime = System.currentTimeMillis(); | |
URL url = new URL(this.ZIP_FILE_URL); | |
url.openConnection(); | |
InputStream reader = url.openStream(); | |
FileOutputStream writer = new FileOutputStream(this.INPUT_ZIP_FILE); | |
byte[] buffer = new byte[102400]; | |
int totalBytesRead = 0; | |
int bytesRead = 0; | |
System.out.println("Reading ZIP file 20KB blocks at a time.\n"); | |
while ((bytesRead = reader.read(buffer)) > 0) { | |
writer.write(buffer, 0, bytesRead); | |
buffer = new byte[102400]; | |
totalBytesRead += bytesRead; | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("Done. " + new Integer(totalBytesRead).toString() + " bytes read (" + new Long(endTime - startTime).toString() + " millseconds).\n"); | |
writer.close(); | |
reader.close(); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void unZipIt() { | |
try { | |
ZipFile zipFile = new ZipFile(INPUT_ZIP_FILE); | |
Enumeration zipEntries = zipFile.entries(); | |
File OUTFILEFOLD = new File(OUTPUT_FOLDER); | |
if (!OUTFILEFOLD.exists()) { | |
OUTFILEFOLD.mkdir(); | |
} | |
String OUTDIR = OUTPUT_FOLDER + File.separator; | |
while (zipEntries.hasMoreElements()) { | |
ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement(); | |
if (zipEntry.isDirectory()) { | |
System.out.println(" Extracting directory: " + OUTDIR + zipEntry.getName()); | |
new File(OUTDIR + zipEntry.getName()).mkdir(); | |
continue; | |
} | |
System.out.println(" Extracting file: " + OUTDIR + zipEntry.getName()); | |
copyInputStream(zipFile.getInputStream(zipEntry), new BufferedOutputStream(new FileOutputStream(OUTDIR + zipEntry.getName()))); | |
} | |
zipFile.close(); | |
} catch (IOException ioe) { | |
System.err.println("Unhandled exception:"); | |
ioe.printStackTrace(); | |
return; | |
} | |
} | |
public static void main(String[] args) { | |
UFG csg = new UFG(); | |
System.out.println("Please provide the S3 file"); | |
Scanner sc = new Scanner(System.in); | |
csg.ZIP_FILE_URL = sc.nextLine(); | |
System.out.println("Please provide the destination with filename"); | |
csg.INPUT_ZIP_FILE = sc.nextLine(); | |
csg.downloadFile(); | |
System.out.println("Provide OUTPUT folder it should unzip to"); | |
csg.OUTPUT_FOLDER = sc.nextLine(); | |
csg.unZipIt(); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you tell the values for those three variables by giving example