Advertisement
cfjedimaster

Untitled

Mar 19th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  6.     <script type="text/javascript" src="zip/zip.js"></script>
  7.     <script type="text/javascript" src="zip/deflate.js"></script>
  8.  
  9.     <script>
  10.     var resourceURL = "resources.zip";
  11.     var resourceDIRLOC = "resources";
  12.     var fileSystem;
  13.     var resourceDIR;
  14.  
  15.     BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder;
  16.  
  17.  
  18.     function errorHandler(e) {
  19.       var msg = '';
  20.  
  21.       switch (e.code) {
  22.         case FileError.QUOTA_EXCEEDED_ERR:
  23.           msg = 'QUOTA_EXCEEDED_ERR';
  24.           break;
  25.         case FileError.NOT_FOUND_ERR:
  26.           msg = 'NOT_FOUND_ERR';
  27.           break;
  28.         case FileError.SECURITY_ERR:
  29.           msg = 'SECURITY_ERR';
  30.           break;
  31.         case FileError.INVALID_MODIFICATION_ERR:
  32.           msg = 'INVALID_MODIFICATION_ERR';
  33.           break;
  34.         case FileError.INVALID_STATE_ERR:
  35.           msg = 'INVALID_STATE_ERR';
  36.           break;
  37.         default:
  38.           msg = 'Unknown Error';
  39.           break;
  40.       };
  41.  
  42.       console.log('Error: ' + msg);
  43.     }
  44.  
  45.     function fetchResource() {
  46.         var xhr = new XMLHttpRequest();
  47. //      xhr.responseType="blob";
  48.         xhr.responseType="arraybuffer";
  49.         xhr.open("GET", resourceURL,true);
  50.         xhr.onload = function(e) {
  51.             if(this.status == 200) {
  52.                 console.log("The last mod for the zip is "+this.getResponseHeader("Last-Modified"));
  53.                 //store the last mod for this file
  54.                 localStorage["resourceLastModified"] = this.getResponseHeader("Last-Modified");
  55.  
  56.                 var bb = new BlobBuilder();
  57.                 bb.append(this.response);
  58.                 var blob = bb.getBlob("application/zip");
  59.  
  60.                 zip.workerScriptsPath = "zip/";
  61.  
  62.                 zip.createReader(new zip.BlobReader(blob), function(reader) {
  63.                     console.log("opened zip");
  64.  
  65.                     reader.getEntries(function(entries) {
  66.                         console.log("total entries "+entries.length);
  67.                         for(var i=0; i<entries.length; i++) {
  68.                             var entry = entries[i];
  69.                             resourceDIR.getFile(entry.filename, {create:true}, function(file) {
  70.                                 console.log("Yes, I opened "+file.fullPath);
  71.                                 entry.getData(new zip.FileWriter(file), function() {
  72.  
  73.                                     // close the zip reader
  74.                                     reader.close(function() {
  75.                                       // onclose callback
  76.                                     });
  77.  
  78.                                   }, function(current, total) {
  79.                                     // onprogress callback
  80.                                   });
  81.  
  82.                             });
  83.  
  84.                         }
  85.  
  86.                     });
  87.                 }, function(err) {
  88.                     console.log("zip reader error!");
  89.                     console.dir(err);
  90.                 })
  91.             }
  92.         }
  93.         xhr.send();
  94.     }
  95.  
  96.     $(document).ready(function() {
  97.  
  98.         window.webkitStorageInfo.requestQuota(window.PERSISTENT, 20*1024*1024, function(grantedBytes) {
  99.             console.log("I was granted "+grantedBytes+" bytes.");
  100.             window.webkitRequestFileSystem(window.PERSISTENT, grantedBytes, onInitFs, errorHandler);
  101.         }, errorHandler);
  102.  
  103.         function onInitFs(fs) {
  104.             console.log("Ok, so see if the file exists...");
  105.             fileSystem = fs;
  106.  
  107.             fileSystem.root.getDirectory(fs.root.fullPath + '/' + resourceDIRLOC, {create:true}, function(dir) {
  108.                 resourceDIR = dir;
  109.  
  110.                 //So we have our resource folder. To determine if our copy is in sync, we check localStorage and compare it to remove file
  111.                 if(localStorage["resourceLastModified"]) {
  112.                     console.log("I had a localStorage value for resourceLastModified, it was "+localStorage["resourceLastModified"]);
  113.                     var xhr = new XMLHttpRequest();
  114.                     xhr.open("HEAD", resourceURL );
  115.                     xhr.onload = function(e) {
  116.                         if(this.status == 200) {
  117.                             if(this.getResponseHeader("Last-Modified") != localStorage["resourceLastModified"]) {
  118.                                 fetchResource();
  119.                             } else {
  120.                                 console.log("Not fetching the zip, my copy is kosher.");
  121.                             }
  122.                         }
  123.                     }
  124.                     xhr.send();
  125.                 } else {
  126.  
  127.                     fetchResource();
  128.                 }
  129.  
  130.             },errorHandler);
  131.         }
  132.  
  133.     });
  134.  
  135.    </script>
  136. </head>
  137.  
  138. <body>
  139. <button id="testButton">Test</button>
  140.  
  141. </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement