none
FilePlus.CompressFile - Problem RRS feed

  • Question

  • Hey guys!

    I tried out the FilePlus.CompressFile option today. But it do not want work. Here is my code:

    a = FilePlus.CompressFile("C:\Users\Timo\Desktop\Tower Defense\Cannon.sb", "C:\Users\Timo\Desktop\Cannon.rar")
    GraphicsWindow.ShowMessage(a, "Ergebnis")

    It creates the Cannon.rar and I can open it with 7zip and winrar but I can't decompress it. I get the message that it is damaged. Any suggestions?

    EDIT: I tried the decompress option too. But there is one problem. It can decompress it, but the file is empty then. I tried it with a .sb file (50 lines code) and as I opened the decompressed file with SmallBasic it was empty!


    Greetings Timo


    • Edited by Timo Sö Saturday, December 8, 2012 1:15 PM
    Saturday, December 8, 2012 1:12 PM

Answers

  • Timo,

    There is a way to do compression using LDProcess.Start.  For a compression function I used windows own "Compact" function.  To see what the commands are for that go into a command prompt window and type compact and enter.  It is a compression utility that is in windows.  You'll need to create 2 .bat files.  I called the first one "Compact.bat" and the second "Uncompact.bat".  I also put a small text file in c:\temp\todo.txt just to test the functions out to be sure it works.  Here's what I put in "Compact.bat":

    compact /c c:\temp\todo.txt

    Here's what I put in "Uncompact.bat":

    compact /u c:\temp\todo.txt

    The /c does the compacting and the /u does the uncompacting.  If you look at the file in the directory when it is compacted it is blue in color and when uncompacted goes back to black.

    Here's the small basic program to compact a file:

    a=LDProcess.start("c:\temp\compact.bat")
    TextWindow.WriteLine(a)

    Here's the small basic program to uncompact a file:

    a=ldprocess.Start("c:\temp\uncompact.bat")
    TextWindow.WriteLine(a)

    Microsoft's compact utility has a lot more functionality.  You could actually use any compression programs that you can use in a command line. 


    JR

    Saturday, December 8, 2012 11:00 PM
    Answerer
  • I added arguments to the LDProcess.Start method and also added some zip compression functions, LDUtilities.Zip and LDUtilities.UnZip.  Not heavily tested, but seems to work, test code below:

    files = File.GetFiles("C:\temp")
    a = LDUtilities.Zip("C:\temp\test.zip",files)
    TextWindow.WriteLine(a)
    a = LDUtilities.UnZip("C:\temp\test.zip","C:\temp\b")
    TextWindow.WriteLine(a)

    Sunday, December 9, 2012 8:17 PM
    Moderator

All replies

  • Timo,

    I set up a small txt file.  Then compressed it.  The compression did work as I can view it's contents which is goobly-gook.  I then tried the decompress function.  It does create the file, but leaves the file empty as you said.  So, it looks like Oskariok has a bug in the decompress function and it is not a problem with what you are trying to do.


    JR

    Saturday, December 8, 2012 3:26 PM
    Answerer
  • Thanks for your try!

    So, do you know another way to compress a direction with all files in it to a .zip or a .rar file with SmallBasic? Perhaps is there another extension with a compress function or a code which uses the windows compress function?

    I will mark your post as answer if I got an answer of my question in this post. Otherwise other users will not look at this I think.


    Greetings Timo


    • Edited by Timo Sö Saturday, December 8, 2012 3:36 PM
    Saturday, December 8, 2012 3:35 PM
  • I also tried a small textfile to compress.

    The resulting file does NOT start with the first two characters PK as all .zip-files do.

    I don't know if the compress finction was ment to make a standard .zip file


    Jan [ WhTurner ] The Netherlands

    Saturday, December 8, 2012 3:59 PM
    Answerer
  • This may be. But I had to choose the compress output path. And this needed a filename with an end (.anything). I only know .zip and .rar. :(


    Greetings Timo

    Saturday, December 8, 2012 4:05 PM
  • Timo,

    There is a way to do compression using LDProcess.Start.  For a compression function I used windows own "Compact" function.  To see what the commands are for that go into a command prompt window and type compact and enter.  It is a compression utility that is in windows.  You'll need to create 2 .bat files.  I called the first one "Compact.bat" and the second "Uncompact.bat".  I also put a small text file in c:\temp\todo.txt just to test the functions out to be sure it works.  Here's what I put in "Compact.bat":

    compact /c c:\temp\todo.txt

    Here's what I put in "Uncompact.bat":

    compact /u c:\temp\todo.txt

    The /c does the compacting and the /u does the uncompacting.  If you look at the file in the directory when it is compacted it is blue in color and when uncompacted goes back to black.

    Here's the small basic program to compact a file:

    a=LDProcess.start("c:\temp\compact.bat")
    TextWindow.WriteLine(a)

    Here's the small basic program to uncompact a file:

    a=ldprocess.Start("c:\temp\uncompact.bat")
    TextWindow.WriteLine(a)

    Microsoft's compact utility has a lot more functionality.  You could actually use any compression programs that you can use in a command line. 


    JR

    Saturday, December 8, 2012 11:00 PM
    Answerer
  • Thanks very much! I will try it today(after coffe and cake with my family)!

    Greetings Timo

    Sunday, December 9, 2012 12:22 PM
  • jricestk is there a was to tell the .bat files that the path starts at their directory? Like in SB with Program.Directory ? I really need this!

    Greetings Timo

    Sunday, December 9, 2012 5:52 PM
  • The compact command is altering files on your NTFS file system, this is not the same as creating a compressed zip file.  Perhaps consider command line options using 7zip or similar.

    Sunday, December 9, 2012 6:17 PM
    Moderator
  • Timo,

    I don't think so.  The reason is LDProcess.start only works when you have:  "compact.bat".  If I put in:  "compact.bat c:\temp" the command fails.  Maybe Litdev can make it work, but currently it doesn't.  The .bat file will accept arguments though. 

    So, the only thing that you can do is to setup the .bat file for what you need.

    Also, some compression programs are really good and others aren't.  The one I used in the example is OK, but there are other ones that do a better job of compressing.  So, you can actually choose what ever program you want to use.


    JR

    Sunday, December 9, 2012 6:19 PM
    Answerer
  • Doesn't the data extension have a process command that takes arguments - mainly why I never bothered updating this?

    If there are problems still I may add arguments to the process control or even a simple compression command, but this probably needs 3rd party software e.g. DotNetZip.

    Sunday, December 9, 2012 6:25 PM
    Moderator
  • I meant, if I can program the bat file to start at the current directory with the file path. I never programmed with .bat before...

    The Data Extension has a compress function. I tried it with the result above.

    I may use 7zip for it, but I want add it to my installer program. So it would be better if I use the windows function. :)

    Thanks for your help so far.


    Greetings Timo

    Sunday, December 9, 2012 7:35 PM
  • I added arguments to the LDProcess.Start method and also added some zip compression functions, LDUtilities.Zip and LDUtilities.UnZip.  Not heavily tested, but seems to work, test code below:

    files = File.GetFiles("C:\temp")
    a = LDUtilities.Zip("C:\temp\test.zip",files)
    TextWindow.WriteLine(a)
    a = LDUtilities.UnZip("C:\temp\test.zip","C:\temp\b")
    TextWindow.WriteLine(a)

    Sunday, December 9, 2012 8:17 PM
    Moderator
  • I think maybe, for the .bat file, you can write this program, run it, and the resulting .bat file will compress the file 'file you want to compress.txt', but I'm not exactly sure. File.WriteContents(Program.Directory+"\Compacting.bat","compact /c "+Program.Directory+"\file you want to compress.txt")

    I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

    Sunday, December 9, 2012 8:27 PM
  • Timo,

    With LitDev's new capability in LDProcess.start loaded (you have to reload the .dll and .xml from LitDev's site).  Here's the programs that do work:

    To compress:

    a=LDProcess.Start("compact", "/c c:\temp\todo.txt")
    TextWindow.WriteLine(a)

    To uncompress:

    a=LDProcess.Start("compact" "/u c:\temp\todo.txt")
    TextWindow.WriteLine(a)

    As before when compressed the file is turned blue, uncompressed goes back to black.

    Thanks LitDev for the new features and your help.  I haven't tried the new zip utility yet.  It's next up!


    JR

    Monday, December 10, 2012 12:43 AM
    Answerer
  • LitDev,

    I tried your new zip utlitiy, but can't get it to work.  Here'st the code i used:

    a=LDUtilities.zip("c:\temp\todo.zip","c:\temp\todo.txt")
    TextWindow.WriteLine(a)

    It does create the zip file, but when I try unzip:

    a=LDUtilities.Unzip("c:\temp\todo.zip","c:\temp\temp")
    TextWindow.WriteLine(a)

    It doesn't put anything in the c:\temp\temp folder.  Also, if I try to extract todo.zip from windows it says it is empty.

    What's wrong?

    Also, what type of zip are you using? 

    Thanks,


    JR

    Monday, December 10, 2012 1:09 AM
    Answerer
  • When you create the zip file, can you check it has anything in it - i.e. open with 7Zip, jZip etc.

    My guess is that the second argument I am using is expecting a SmallBasic array of files.  Can you test by creating a single file in an array and passing this as the second argument works.

    files[1] = "C:\temp\todo.txt"

    If this works then I will modify the code to allow the second argument to be a single file.

    The TextWindow.WriteLine(a) should print any error that prevented it working - like file permissions, non existant files or others.

    I am using some rather convoluted code to do the compression using .Net, I wanted to stay away from third party dll if possible - the compression type is "Normal", but I don't know how general the decompression will be, assuming you can get it to work at all!

    Monday, December 10, 2012 10:22 AM
    Moderator
  • Thanks LitDev! It works well without arrays. With arrays it will zip and unzip the first array correctly, but the second one only had the .bmp file (I puted in every folder 1 .txt file, 1 .docx file, 1 .bmp file and 1.rar file). Next in every zip archiv was a file called [Content_Types] and this was in it:

    <?xml version="1.0" encoding="UTF-8"?>
    -<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default ContentType="application/zip" Extension="bmp"/></Types>

    That's my code:

    files[1] = File.GetFiles("C:\Users\Timo\Desktop\a")
    files[2] = File.GetFiles("C:\Users\Timo\Desktop\b")
    
    a[1] = LDUtilities.Zip("C:\Users\Timo\Desktop\testa.zip",files[1])
    a[2] = LDUtilities.Zip("C:\Users\Timo\Desktop\testb.zip",files[2])
    
    b[1] = LDUtilities.UnZip("C:\Users\Timo\Desktop\testa.zip","C:\Users\Timo\Desktop\a c")
    b[2] = LDUtilities.UnZip("C:\Users\Timo\Desktop\testb.zip","C:\Users\Timo\Desktop\b c")

    Really thank you! I can go on working now. :)

    EDIT: LitDev, is there a way to tell your zip task to zip EVERYTHING in the directory? I tried it with a mix of directories and files but it only zip the the files in the main directory. My code:

    ProgName = "Test"
    files = File.GetFiles(Program.Directory + "\Data")
    TextWindow.WriteLine(files) 'only for control
    Zip = LDUtilities.Zip(Program.Directory + "\" + ProgName + ".zip", files)


    Greetings Timo



    • Edited by Timo Sö Monday, December 10, 2012 1:01 PM
    Monday, December 10, 2012 11:38 AM
  • I think this is because the File.GetFiles only gets the files, not directories - try adding the directory to the list of files array and see what happens.
    Monday, December 10, 2012 1:13 PM
    Moderator
  • I tried it, but there was a problem. It added a file for the sub directory, but it was no folder. It was an undefined file with a size of 0 Bytes. So may you fix this please? Perhaps you can add that everything in the direction will be compressed. Like this (It does not work at the moment):

      Zip = LDUtilities.Zip(Program.Directory + "\" + ProgName + ".zip", Program.Directory + "\Data") 'So insteat of 'files' a written path.


    Greetings Timo

    Monday, December 10, 2012 1:46 PM
  • I will have a look - I expect I would have to manually write a recursive file finder for all directories, then there may be issues with files of the same name in different directories and other issues.  Will check, but it may just be a big can of worms.

    Monday, December 10, 2012 4:01 PM
    Moderator
  • For the start it would be enough if it can do it. You could write in the .xml file that it become an issue if there are files with the same name. The you lose a ton of work I think. :)

    Thanks in advance. :)


    Greetings Timo

    Monday, December 10, 2012 4:24 PM
  • Modified version uploaded (file recursion not too hard - no problem with same name files in different folders). 

    For the Zip function:

    1] Either an array of files/directories or a single file/directory can be specified

    2] Any directories will be recursively added

    The only odd thing I see is that any white space in files or directories has to converted to an underscore "_".

    Also, if the zip file already exists, any zip files will be appended, not a new archive, so delete it first if you don't want to append.
    Monday, December 10, 2012 6:50 PM
    Moderator
  • LitDev,

    I did my "ToDo.txt" file.  It did create "ToDo.zip".  In the .zip file there is another file [Content_Type].xml].  The inside of it is:

    <?xml version="1.0" encoding="UTF-8"?>
    -<Types xmlns="<Default">http://schemas.openxmlformats.org/package/2006/content-types"><Default ContentType="application/zip" Extension="txt"/></Types>

    I see this when I double click the .zip file.  The Unzip works correctly and only puts the one file in the designated folder.  Should that extra file be there in the archive though?

    Great job on getting this working.  I certainly wouldn't know how to do it.  It is a good feature to have in SB.


    JR


    Monday, December 10, 2012 10:43 PM
    Answerer
  • I saw that too, the zip mechanism seems to put it there automatically detailing some of the files types in the archive. 

    If it works then probably just leave it as it is.

    You can always play with the source code and read up on the .Net methods used and see if there is anything that could be done.

    Tuesday, December 11, 2012 9:14 AM
    Moderator
  • Thanks very much LitDev! It works very well now.

    Greetings Timo

    Tuesday, December 11, 2012 3:14 PM