SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create an archive file without components?

    Is there a script or a simple way(I doubt), to create archive files online without components...only with asp? by "archive files"....I mean .zip files or something similar that can then be de-archived later? It is not important whether the files get compressed or not.

    I searched for a similar script all day without result.

    Thanks

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In theory I guess you could use the FielSystemObject to read each file and write its contents to another file, and then write a "separator" line/mark between each file segment. However you could end up with a very large file (potentially) and you might run into scripting timeout problems. You would also have to contemplate non-text files (e.g. images).

    Other than this, maybe try searching at www.hotscripts.com
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, a technique that I used a while ago is to create XML documents and store the files as binary data in nodes of datatype base64, and the filenames as attributes or text nodes. Then parsing the XML doc and reconstructing the files is easy. My implementation also employed encryption, but that's probably not necessary for your needs...

    The clear problem with this technique is that the resulting XML docs are bigger than the sum of the original file sizes (approx. 10% bigger or so), but the fact that you don't care about compression suggests that the size of these "archive" files is irrelevant.

    Does that help? I can also post some code to give you a starting point (if you need it)...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that would be very nice, if you could post some code to give me a start....since I'm not very familiar with XML. I'll then figure it out by myself. Just whenever you have a lot of time to spend, I'm not in a hurry

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you do a search for zip libraries. I'm don't now any ASP, but I suppose you'd be able to find a library that allows you to create zip archives.

    There's one in PHP, which is included in phpMyAdmin, that I've found very useful and easy to use.

    I'll post the relevant archiving method, in case it's of any help.
    PHP Code:
        /**
         * Adds "file" to archive
         *
         * @param  string   file contents
         * @param  string   name of the file in the archive (may contains the path)
         * @param  integer  the current timestamp
         *
         * @access public
         */
        
    function addFile($data$name$time 0)
        {
            
    $name     str_replace('\\''/'$name);

            
    $dtime    dechex($this->unix2DosTime($time));
            
    $hexdtime '\x' $dtime[6] . $dtime[7]
                      . 
    '\x' $dtime[4] . $dtime[5]
                      . 
    '\x' $dtime[2] . $dtime[3]
                      . 
    '\x' $dtime[0] . $dtime[1];
            eval(
    '$hexdtime = "' $hexdtime '";');

            
    $fr   "\x50\x4b\x03\x04";
            
    $fr   .= "\x14\x00";            // ver needed to extract
            
    $fr   .= "\x00\x00";            // gen purpose bit flag
            
    $fr   .= "\x08\x00";            // compression method
            
    $fr   .= $hexdtime;             // last mod time and date

            // "local file header" segment
            
    $unc_len strlen($data);
            
    $crc     crc32($data);
            
    $zdata   gzcompress($data);
            
    $zdata   substr(substr($zdata0strlen($zdata) - 4), 2); // fix crc bug
            
    $c_len   strlen($zdata);
            
    $fr      .= pack('V'$crc);             // crc32
            
    $fr      .= pack('V'$c_len);           // compressed filesize
            
    $fr      .= pack('V'$unc_len);         // uncompressed filesize
            
    $fr      .= pack('v'strlen($name));    // length of filename
            
    $fr      .= pack('v'0);                // extra field length
            
    $fr      .= $name;

            
    // "file data" segment
            
    $fr .= $zdata;

            
    // "data descriptor" segment (optional but necessary if archive is not
            // served as file)
            
    $fr .= pack('V'$crc);                 // crc32
            
    $fr .= pack('V'$c_len);               // compressed filesize
            
    $fr .= pack('V'$unc_len);             // uncompressed filesize

            // add this entry to array
            
    $this -> datasec[] = $fr;
            
    $new_offset        strlen(implode(''$this->datasec));

            
    // now add to central directory record
            
    $cdrec "\x50\x4b\x01\x02";
            
    $cdrec .= "\x00\x00";                // version made by
            
    $cdrec .= "\x14\x00";                // version needed to extract
            
    $cdrec .= "\x00\x00";                // gen purpose bit flag
            
    $cdrec .= "\x08\x00";                // compression method
            
    $cdrec .= $hexdtime;                 // last mod time & date
            
    $cdrec .= pack('V'$crc);           // crc32
            
    $cdrec .= pack('V'$c_len);         // compressed filesize
            
    $cdrec .= pack('V'$unc_len);       // uncompressed filesize
            
    $cdrec .= pack('v'strlen($name) ); // length of filename
            
    $cdrec .= pack('v');             // extra field length
            
    $cdrec .= pack('v');             // file comment length
            
    $cdrec .= pack('v');             // disk number start
            
    $cdrec .= pack('v');             // internal file attributes
            
    $cdrec .= pack('V'32 );            // external file attributes - 'archive' bit set

            
    $cdrec .= pack('V'$this -> old_offset ); // relative offset of local header
            
    $this -> old_offset $new_offset;

            
    $cdrec .= $name;

            
    // optional extra field, file comment goes here
            // save to central directory
            
    $this -> ctrl_dir[] = $cdrec;
        } 
    // end of the 'addFile()' method 

  6. #6
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tabsquare - Here's the core functions that my system used, which I think you should be able to used without much (or indeed any) tweaking...
    Code:
    Function BuildXMLDoc(FilePathArray)
    	'create XML document with default header and primary node
    	set XMLDOM = CreateObject("MSXML2.DOMDocument")
    	XMLDOM.loadXML "<?xml version=""1.0"" ?> <ROOT/>"
    	
    	'specify namespaces datatypes
    	XMLDOM.documentElement.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"
    
    	FilePathArrayLength = UBound(FilePathArray)
    	
    	for n=0 to FilePathArrayLength
    		FilePath = FilePathArray(n)
    		FileName = mid(FilePath,instrrev(FilePath,"\")+1)
    		
    		'create a new node and set binary content
    		set FileNode = XMLDOM.createElement("file")
    		FileNode.setAttribute "filename", FileName
    		FileNode.dataType = "bin.base64"
    		
    		'store file content into XML node
    		FileNode.nodeTypedValue = LoadStream(FilePath)
    		
    		'append the node to the XML root node
    		XMLDOM.documentElement.appendChild FileNode
    	next
    
    	BuildXMLDoc = XMLDOM.xml
    End Function
    
    Function LoadStream(FilePath)
    	Dim objStream
    	Set objStream = CreateObject("ADODB.Stream")
    
    	objStream.Type = 1 'adTypeBinary
    	objStream.Open
    
    	objStream.LoadFromFile FilePath
    	LoadStream = objStream.Read
    	
    	objStream.Close
    	Set objStream = Nothing
    End Function
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the function works perfectly, I only need to modify it a bit to make it also write the empty folders' paths in the XML file. This is because I also need to recreate the entire folder structure after the XML file has been generated.

    Thank you very much

  8. #8
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome!

    Just one thing - I originally wrote that code so that it could be used in both server-side and client-side VBScript, but if you're only using it for server-side, then I suggest that you change the CreateObject() calls to Server.CreateObject(). Little difference in real terms apart from better memory management (i.e. garbage collection), etc...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •