1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Call Zipfile from Remote Server, and extract it.

Discussion in 'Other Languages' started by adbox, Feb 4, 2010.

  1. adbox

    adbox Power Member

    Joined:
    May 1, 2009
    Messages:
    658
    Likes Received:
    107
    Home Page:
    Hey guys,

    I am looking for an auto update solution for my software. I want to call a zip file from a remote server, download it, and then extract it's contents replacing old contents.

    Anyone have a solution for me?

    A total package would include a progress bar.

    thanks!
    adbox
     
  2. Choronzon

    Choronzon Power Member

    Joined:
    Apr 20, 2008
    Messages:
    559
    Likes Received:
    376
    What language is your software written in?
     
  3. adbox

    adbox Power Member

    Joined:
    May 1, 2009
    Messages:
    658
    Likes Received:
    107
    Home Page:
    hey, php
     
  4. matessim

    matessim Junior Member

    Joined:
    Nov 22, 2008
    Messages:
    164
    Likes Received:
    72
    Occupation:
    Being funny and kind to puppies
    Location:
    UT 2003
    what language are you implementing in?...
     
  5. webinteger

    webinteger Newbie

    Joined:
    Nov 18, 2012
    Messages:
    1
    Likes Received:
    0
    1. PHP file download phpriot_com/articles/download-with-curl-and-php
    2. PHP zip class phpclasses_org/package/6110
    to say something rather need more information
     
    Last edited: Nov 19, 2012
  6. queenmery

    queenmery Power Member

    Joined:
    Jan 18, 2011
    Messages:
    501
    Likes Received:
    30
    Occupation:
    Student
    Location:
    BANGLADESH
    I could not get your problem properly so if you can explain this a bit more then we will be able help you further.
     
  7. stfuplease

    stfuplease Newbie

    Joined:
    Jun 25, 2013
    Messages:
    20
    Likes Received:
    24
    Wow, so I just wrote a nice script to do exactly what you wanted, only to realize this post is from 2012. Then I scrolled up a little bit more and realized it's actually from 2010. I'm posting it anyway since a) I invested the time, b) it works and c) someone else may find it useful.


    First, it operates under a couple of assumptions. For starters, each version needs to have CURRENT_VERSION defined at the very top of the script. Secondly, it assumes that all updates are released in the same exact format (i.e. project_1.3.zip) on the same exact domain (e.g. yourdomain.com) in the same exact directory (e.g. /updates/) every time. Modifying it to use a different method of detection would be relatively easy. For example, you could use the remote_file_get_contents() function to fetch current.txt, which could contain the most recent version number and a link to it (and then the script could parse and use that information instead). Anyway, the code:
    Code:
    <?php
    /[b][/b]/ CURRENT VERSION, MAKE SURE TO UPDATE WITH EACH RELEASE
    define("CURRENT_VERSION", "1.4");
    /[b][/b]/ CHECK FOR UPDATES
    autoUpdate();
    
    
    /[b][/b]/ AUTOMATICALLY CHECKS FOR UPDATES AND PERFORMS THEM IF FOUND
    function autoUpdate() {
    	/[b][/b]/ TEMP FILENAME FOR ZIP
    	$tempFile    = uniqid("temp_")[b][/b].".[b][/b]zi[b][/b]p";
    	/[b][/b]/ INCREMENT VERSION NUMBER
    	$nextVersion = (CURRENT_VERSION+1);
    	/[b][/b]/ CHECK TO SEE IF IT EXISTS
    	$nextURL 	 = "ht[b][/b]tp[b][/b]:/[b][/b]/[b][/b]yourdomain[b][/b].[b][/b]co[b][/b]m[b][/b]/[b][/b]updates/[b][/b]project_$nextVersion[b][/b].[b][/b]zi[b][/b]p";
    	/[b][/b]/ IF IT EXISTS, THEN PERFORM UPDATE
    	if (remote_file_exists($nextURL)) {
    		/[b][/b]/ DISPLAY DEBUG
    		print("Update found, downloading\r\n");
    		/[b][/b]/ FETCH THE REMOTE DATA
    		$zipData = remote_file_get_contents($nextURL);
    		/[b][/b]/ SAVE IT LOCALLY
    		file_put_contents($tempFile, $zipData);		
    		/[b][/b]/ EXTRACT IT
    		$zipData = extract_archive($tempFile);
    		
    	} else {
    		/[b][/b]/ INFORM USER OF NO NEW UPDATES
    		print("No new updates\r\n");
    	}	
    }
    
    
    /[b][/b]/ EXTRACT THE ARCHIVE
    function extract_archive($tempFile) {
    	/[b][/b]/ CREATE NEW ARCHIVE HANDLE
    	$zip = new ZipArchive;
    	/[b][/b]/ OPEN TEMP ZIP FILE
    	$res = $zip->open($tempFile);
    	/[b][/b]/ IF IT FAILED, THROW ERROR AND DIE
    	if ($res !== TRUE) { die("\r\nError: unable to perform update, zip corrupt?\r\n"); }
    	/[b][/b]/ EXTRACT IT TO THE CURRENT DIRECTORY
    	$zip->extractTo('.');
    	/[b][/b]/ CLOSE HANDLE
    	$zip->close();
    	/[b][/b]/ REMOVE THE TEMP FILE
    	unlink($tempFile);
    	/[b][/b]/ SHOW DEBUG
    	printf("Update successfully performed!\r\n");
    }
    
    
    
    
    /[b][/b]/ DOWNLOAD REMOTE FILE
    function remote_file_get_contents($sURL) {
    	/[b][/b]/ CREATE HANDLE TO CURL
    	$ch = curl_init($sURL);
    	/[b][/b]/ SET OPTIONS, EXECUTE REQUEST AND CLOSE HANDLE
    	$c  = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	$c  = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    	$x  = curl_exec($ch);
    	$c  = curl_close($ch);
    	/[b][/b]/ BUILD RETURN VALUE
    	$bRet = (strstr($x, "200 OK")) ? true : false;
    	/[b][/b]/ AND RETURN IT
    	return($bRet);
    }
    
    
    /[b][/b]/ CHECK IF REMOTE FILE EXISTS
    function remote_file_exists($sURL) {
    	/[b][/b]/ CREATE HANDLE TO CURL
    	$ch = curl_init($sURL);
    	/[b][/b]/ SET OPTIONS, EXECUTE REQUEST AND CLOSE HANDLE
    	$c  = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	$c  = curl_setopt($ch, CURLOPT_NOBODY, true);
    	$c  = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    	$c  = curl_setopt($ch, CURLOPT_HEADER, true);
    	$x  = curl_exec($ch);
    	$c  = curl_close($ch);
    	/[b][/b]/ BUILD RETURN VALUE
    	$bRet = (strstr($x, "200 OK")) ? true : false;
    	/[b][/b]/ AND RETURN IT
    	return($bRet);
    }
    ?>
    Assuming that I understood you correctly, this would do exactly what you want. On each execution, it'd issue a HEAD request (no point in downloading the response until we know for sure that the file exists/an update is available) likely near-instant on each page load/script execution (and this could be cached, e.g. use memcache or apc to make autoUpdate() only callable once every 12 hours). If an update exists (a newer .zip was found), then that file is automatically downloaded, saved and then extracted to the current directory (.), which would overwrite any already existing files (thus performing an update). Unmodified files would remain as they were prior to the update. Then, assuming that the new.php file has an updated version (e.g. 1.5) defined in it - no more updates would be performed until project_1.6.zip was created.

    There are obvious drawbacks, though. For example, if the user hasn't updated in a while and is running ver 1.3 and the project is up to ver 1.8 - then the script WILL get to the latest update, but it will take 5 separate updates for that to happen. In addition, the outcome could become unpredictable if the script is executed at the same exact time by two different people. So you'd want to place a lock (e.g. memcache or apc variable, or even a local file) indicating that "hey, i'm updating, FREEZE until i say i'm done or you timeout on your own" so that you don't risk overwriting files that are currently being overwritten (and potentially corrupting them).
     
    • Thanks Thanks x 1