(Java) Unzip Text File to String
Demonstrates how to open a .zip and extract the 1st file (assuming it's a text file) to a string variable.
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkZip zip = new CkZip();
boolean success = zip.OpenZip("qa_data/zips/EC16100.zip");
if (success != true) {
System.out.println(zip.lastErrorText());
return;
}
// Get the 1st file in the .zip
CkZipEntry entry = zip.GetEntryByIndex(0);
if (zip.get_LastMethodSuccess() != true) {
System.out.println("No files in this zip.");
return;
}
String fileContents = entry.unzipToString(0,"utf-8");
System.out.println(fileContents);
}
}
|