package zip
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.filesystem.File;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.net.URLLoader;
import flash.net.URLStream;
import flash.utils.ByteArray;
import deng.fzip.FZip;
import deng.fzip.FZipFile;
import Alert;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

public class ZIPResourceLoader
{
public var resourcesURL:String = "https://redagereborn.ru/resources.zip";
public var versionURL:String = "http://127.0.0.1:8000/version.txt";
public var localFilePath:String = File.applicationStorageDirectory.nativePath + File.separator + "resources.zip";
public var versionFile:File = new File(File.applicationStorageDirectory.nativePath + File.separator + "version.txt");
public var zipLoader:URLLoader = new URLLoader();

public function ZIPResourceLoader()
{
zipLoader.dataFormat = URLLoaderDataFormat.TEXT;
zipLoader.addEventListener(Event.COMPLETE, onVersionLoaded);
zipLoader.addEventListener(IOErrorEvent.IO_ERROR, onVersionLoadError);
zipLoader.load(new URLRequest(versionURL));
}

public function onVersionLoaded(event:Event):void
{
var version:Number = Number(zipLoader.data);

if (versionIsUpToDate(version)) {
Alert.showMessage("Using local resources.zip");
extractLocalArchive();
} else {
Alert.showMessage("Downloading resources.zip");
var downloadStream:URLStream = new URLStream();
downloadStream.addEventListener(Event.COMPLETE, onDownloadComplete);
downloadStream.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
downloadStream.load(new URLRequest(resourcesURL));
}
}

public function onVersionLoadError(event:IOErrorEvent):void
{
Alert.showMessage("Failed to load version.txt");
}

public function onDownloadComplete(event:Event):void
{
var downloadStream:URLStream = event.target as URLStream;
var fileBytes:ByteArray = new ByteArray();
downloadStream.readBytes(fileBytes);

var fileStream:FileStream = new FileStream();
fileStream.open(new File(localFilePath), FileMode.WRITE);
fileStream.writeBytes(fileBytes, 0, fileBytes.length);
fileStream.close();

Alert.showMessage("Downloaded resources.zip");

extractLocalArchive();
}

public function onDownloadError(event:IOErrorEvent):void
{
Alert.showMessage("Failed to download resources.zip");
}

public function extractLocalArchive():void
{
var zipFile:FZip = new FZip();
zipFile.addEventListener(Event.COMPLETE, onZipExtracted);
zipFile.load(new URLRequest(localFilePath));
}

public function onZipExtracted(event:Event):void
{
var zipFile:FZip = event.target as FZip;

try {
for (var i:int = 0; i < zipFile.getFileCount(); i++)
{
var zipEntry:FZipFile = zipFile.getFileAt(i);
var targetFilePath:String = File.applicationStorageDirectory.nativePath + File.separator + "cache/resources" + File.separator + zipEntry.filename;
var targetFile:File = new File(targetFilePath);

if (zipEntry.filename.charAt(zipEntry.filename.length - 1) == "/") {
targetFile.createDirectory();
} else {
var targetFileStream:FileStream = new FileStream();
targetFileStream.open(targetFile, FileMode.WRITE);
targetFileStream.writeBytes(zipEntry.content);
targetFileStream.close();
}
}

Alert.showMessage("Extracted successfully!");
} catch (error:Error) {
Alert.showMessage("Failed to extract resources.zip: " + error.message + " (" + error.errorID + ")");
}
}

private function versionIsUpToDate(version:Number):Boolean
{
if (versionFile.exists) {
var fileStream:FileStream = new FileStream();
fileStream.open(versionFile, FileMode.READ);
var localVersion:Number = Number(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();

return version <= localVersion;
}

return false;
}
}
} так, но он не зависимо от того какая цифра стоит в txt файле все равно качает допустим стояло 1, а он скачал, перезашел он все равно продолжает качать