Java
Chilkat2 Java
Download a Zip from a URL and OpenBd. (No .zip file is created)
See more Zip Examples
Demonstrates how to download a .zip from a URL, opens the Zip, and gets the contents of a file. No file is ever written.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Zip;
import com.chilkatsoft.chilkat2.ZipEntry;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
BinData bd = new BinData();
// This URL is valid and can be tested...
try {
http.quickGetBd("https://chilkatdownload.com/example_data/hamlet.zip", bd);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Zip zip = new Zip();
// Open the zip from the bytes contained in bd.
try {
zip.openBd(bd);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the entry for the file we want..
ZipEntry entry = new ZipEntry();
success = zip.entryOf("hamlet.xml", entry);
if (!success) {
System.out.println(zip.getLastErrorText());
return;
}
// Convert all line endings to CRLF (if needed)
int lineEndingBehavior = 2;
String xmlStr;
try {
xmlStr = entry.unzipToString(lineEndingBehavior, "utf-8");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The XML in this case is about 274K, so let's just examine the last 20 lines...
StringBuilder sb = new StringBuilder();
sb.append(xmlStr);
System.out.println(sb.lastNLines(20, true));
}
}