Our external application sends the zip file name and content as two strings in the response to an API call. I converted the string to bytearray and used zipinputstream to read the bytearray. If the zip file contains only one file, I am able to read the string and extract the zipped file contents into a separate string. Below is the code snippet I used to extract the file content.
If the zipped file contains more than one file and the entire zipped content is sent as a single string, how do I extract individual file contents into separate strings? How to identify the end of each file in the string?
byte[] data = Base64Util.decodeToByteArray(ZipString);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(bais));
ZipEntry ze = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[4096];
String ZippedFileName = new String();
String ZippedFileData = new String();
try
{
while((ze = zin.getNextEntry()) != null)
{
ZippedFileName = ze.getName();
while((zin.read(b,0,4096))!= -1)
{
baos.write(b,0,4096);
}
}
byte[] out = baos.toByteArray();
ZippedFileData = Base64Util.encodeToString(out);
zin.close();
bais.close();
baos.close();
}
catch(Exception Ex)
{
Ex.toString();
}
Actually I think I was presenting the base64 file in string form incorrectly. For one thing, if line feeds are present in the input, it doesn't like it. If you're working on a Unix based system, you can run the below with something like java Unzip64
The following is working fine for me
And here's a sample to run: UEsDBAoAAAgAAKdCJ1UAAAAAAAAAAAAAAAAHAAQAcm9vdC9hL/7KAABQSwMEFAAICAgAp0InVQAAAAAAAAAAAAAAAAwAAAByb290L2EvYS50eHRL5AIAUEsHCAeh6t0EAAAAAgAAAFBLAwQUAAgICACnQidVAAAAAAAAAAAAAAAADAAAAHJvb3QvYS9iLnR4dEviAgBQSwcIxPLH9gQAAAACAAAAUEsDBBQACAgIAKdCJ1UAAAAAAAAAAAAAAAAMAAAAcm9vdC9hL2MudHh0S+YCAFBLBwiFw9zvBAAAAAIAAABQSwECCgAKAAAIAACnQidVAAAAAAAAAAAAAAAABwAEAAAAAAAAAAAAAAAAAAAAcm9vdC9hL/7KAABQSwECFAAUAAgICACnQidVB6Hq3QQAAAACAAAADAAAAAAAAAAAAAAAAAApAAAAcm9vdC9hL2EudHh0UEsBAhQAFAAICAgAp0InVcTyx/YEAAAAAgAAAAwAAAAAAAAAAAAAAAAAZwAAAHJvb3QvYS9iLnR4dFBLAQIUABQACAgIAKdCJ1WFw9zvBAAAAAIAAAAMAAAAAAAAAAAAAAAAAKUAAAByb290L2EvYy50eHRQSwUGAAAAAAQABADnAAAA4wAAAAAA