2

I am trying to get the compressed ZIP file back in Javascript. I am able to convert the zip file into Base64 String format. (Zip file is in Server)

Here is my try (at Server Side)

System.IO.FileStream fs = new System.IO.FileStream(SourceFilePath + "Arc.zip", System.IO.FileMode.Open);
Byte[] zipAsBytes = new Byte[fs.Length];
fs.Read(zipAsBytes, 0, zipAsBytes.Length);
String base64String = System.Convert.ToBase64String(zipAsBytes, 0, zipAsBytes.Length);
fs.Close();

if (zipAsBytes.Length > 0)
{
    _response.Status = "ZipFile";
    _response.Result = base64String;
}

return _json.Serialize(_response);

This part of code returns the JSON data. This JSON data includes the Base64 string. Now what i want to do is to get the original zip file from Base64 string. I searched over the internet but not get the idea.

Is this achievable ?.

mtmk
  • 6,176
  • 27
  • 32
Prabhu Easwar
  • 153
  • 1
  • 2
  • 10
  • `System.IO.FileStream fs = new System.IO.FileStream(SourceFilePath + "Arc.zip", System.IO.FileMode.Open);` <-- That does not look like JavaScript. – Cerbrus Jul 02 '14 at 13:41
  • What do you man by the get the original zip file? Do you want to extract the contents in the browser? or Store the data as a zip file locally? – levi Jul 02 '14 at 13:48
  • @Cebrus: The code i posted here is the try at Server Side. Base64 conversion of ZIP file. – Prabhu Easwar Jul 02 '14 at 13:52
  • @levi: second question is the preferred one. I just want to store the zip file in local device. For future use. – Prabhu Easwar Jul 02 '14 at 13:54
  • For future use by your JavaScript? or by the user? you would need to put the content into local storage else how would you access it through a script? How big is the uncompressed data? – Alex K. Jul 02 '14 at 13:57
  • in that case, than why send it as Base64 Json? You can just download the zip directly. See - http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php – levi Jul 02 '14 at 13:58
  • The zip file is later on used by the JS functions. If the time come to use then that Base64 string is need to convert to the ZIP file. ( JS use the content inside the zip). Once used then zip file will be getting deleted. – Prabhu Easwar Jul 02 '14 at 14:06

2 Answers2

3

It is achievable. First you must convert the Base64 string to an Arraybuffer. Can be done with this function:

function base64ToBuffer(str){
    str = window.atob(str); // creates a ASCII string
    var buffer = new ArrayBuffer(str.length),
        view = new Uint8Array(buffer);
    for(var i = 0; i < str.length; i++){
        view[i] = str.charCodeAt(i);
    }
    return buffer;
}

Then, using a library like JSZip, you can convert the ArrayBuffer to a Zip file and read its contents:

var buffer = base64ToBuffer(str);
var zip = new JSZip(buffer);
var fileContent = zip.file("someFileInZip.txt").asText();
levi
  • 23,693
  • 18
  • 59
  • 73
-1
  • JavaScript does not have that functionality.
  • Theoretically there can be some js library that does this, but it's size probably would be bigger than the original text file itself.
  • You can also enable gzip compression on your server, so that any output text gets compressed. Most of the browsers would then uncompress the data upon its arrival.
Vlas Bashynskyi
  • 1,886
  • 2
  • 16
  • 25