www.webdeveloper.com
Results 1 to 7 of 7

Thread: get filetype from string and unzip

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    6

    get filetype from string and unzip

    Hi Folks,

    I'm reading files of a directory into a list and then loop through it. with every loopcount I get one filename, then if it is a xlf file, I insert the data into my db. But if the file is a zip file (always containing serveral xls files), and here is my problem, it has to be unziped first, and then another loop has to get over the unziped files.

    So how can I see if my file is a zip file?
    And how can I unzip a zip file? (with coldfusion 7)

    The only solutions I found were for ColdFusion 9, because there is a unzip function, but I have to work with ColdFusion 7 Server.

    Greets

    JollyRoger

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Can you install CFX objects to the server?

    http://www.emteksys.com/products/cfxzip/

  3. #3
    Join Date
    Apr 2011
    Posts
    6
    no I don't have permission to install anything.
    so perhaps I have to think about a php workaround? perhaps a php document that extracts the zip files into the same directory and then opens my cfm page, which loops over all csv files in a directory.

    That leads us to my second problem, I don't understand the use of regex in CFML

    can you give me the right syntax for:
    <cfif #filename# EQ *.zip>

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Take a look at this: http://www.cflib.org/index.cfm?event...byid&udfid=997

    Now, to answer your question:
    Assuming that the CFDIRECTORY is giving you the filename, you set the filter to "zip" and it will give you ONLY .zip files.

    Otherwise, if the filename is in #filename#, then you use ListLast to get the file extension:
    cfif ListLast(filename,".") eq 'zip'
    do stuff if zip
    else
    do stuff for not zip
    cfif

  5. #5
    Join Date
    Apr 2011
    Posts
    6
    first the zip file filter works perfect, it tried to put several files from different types into one dir and cfdirectory gave me only the zip files, thanks for that

    the unzip method you linked looked easy, but I don't get it working.
    I don't know how to call the javascript function right.
    I added an alert, but it didn't come up...

    can you tell me what I did wrong??

    Code:
    <html>
    <head>
    	<title>Zip Dateien</title>
    <script>
    /**
    * Unzips a file to the specified directory.
    *
    * @param zipFilePath      Path to the zip file (Required)
    * @param outputPath      Path where the unzipped file(s) should go (Required)
    * @return void
    * @author Samuel Neff (sam@serndesign.com)
    * @version 1, September 1, 2003
    */
    function unzipFile(zipFilePath, outputPath) {
    	alert(zipFilePath);
        var zipFile = ""; // ZipFile
        var entries = ""; // Enumeration of ZipEntry
        var entry = ""; // ZipEntry
        var fil = ""; //File
        var inStream = "";
        var filOutStream = "";
        var bufOutStream = "";
        var nm = "";
        var pth = "";
        var lenPth = "";
        var buffer = "";
        var l = 0;
    
        zipFile = createObject("java", "java.util.zip.ZipFile");
        zipFile.init(zipFilePath);
        
        entries = zipFile.entries();
        
        while(entries.hasMoreElements()) {
            entry = entries.nextElement();
            if(NOT entry.isDirectory()) {
                nm = entry.getName();
                
                lenPth = len(nm) - len(getFileFromPath(nm));
                
                if (lenPth) {
                pth = outputPath & left(nm, lenPth);
            } else {
                pth = outputPath;
            }
            if (NOT directoryExists(pth)) {
                fil = createObject("java", "java.io.File");
                fil.init(pth);
                fil.mkdirs();
            }
            filOutStream = createObject(
                "java",
                "java.io.FileOutputStream");
            
            filOutStream.init(outputPath & nm);
            
            bufOutStream = createObject(
                "java",
                "java.io.BufferedOutputStream");
            
            bufOutStream.init(filOutStream);
            
            inStream = zipFile.getInputStream(entry);
            buffer = repeatString(" ",1024).getBytes();
            
            l = inStream.read(buffer);
            while(l GTE 0) {
                bufOutStream.write(buffer, 0, l);
                l = inStream.read(buffer);
            }
            inStream.close();
            bufOutStream.close();
            }
        }
        zipFile.close();
    }
    </script>		
    </head>
    <body>
    <cfoutput>
    	
    
    	
    <h3>Verzeichnisinformationen auflisten</h3>
    
    
    <cfset path = '../../premium_adressen/#DateFormat(Now(),"YYYY-MM-DD")#'>
    
    
    <cfdirectory
      directory="#ExpandPath(path)#"
      name="premium_adressen"
      filter="*.zip">
    
    <cfloop query="premium_adressen">
    
    <cfset absolut_path="#ExpandPath(path)#">
    <cfset zipfile = "#ExpandPath(path)#/#premium_adressen.name#">
    <script>
    unzipFile("#zipfile#", "#absolut_path#");
    </script>
    	
    </cfloop>
    
    </cfoutput>
    </body>

  6. #6
    Join Date
    Apr 2011
    Posts
    6
    ok it seems to work now with <cfscript> instead of <script>

  7. #7
    I also don't have permission to install it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles