locked
Compression Check RRS feed

  • Question

  • Hi all,

     

    I'm busy with some new idea's to make my life again a bit easier at work and this time i'm searching for a way to check to make a program in VB.net that checks a file or a folder with files in it. And that shows me how much compression i can do on it. example file X.X is 1000 MB and maximum compression could be 70 % so then i know if i will compress the file i get a new file of 300 MB. (roughly)

     

    I wonder if it is possible to do this without compressing the files fist.

     

    Hope to get some interesting answers.

     

    Kind Regards,

    Michael

    Friday, March 11, 2011 3:47 PM

Answers

All replies

  • I wonder if it is possible to do this without compressing the files fist.
    No.
    Friday, March 11, 2011 4:18 PM
  • You can try to compress it in memory without write it to disk but there may be size limit for memory stream compression.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, March 11, 2011 5:32 PM
  • John: Thanks for that like i already tought its not possible.

     

    KayMaf: Your answer is interesting if it should be possible to compress in memory and calculate there the comppression without to write the filde to the harddisk to get results would be fantastic. But maybe i should create it then like if there is enough free memory to do it in memory else create a tmp folder on a disk with atleast the space of the file size to compress and then after the results are there delete the tmp folder

    But like you already said there maybe size limit for memory stream compression and if not then its more the question how to do it.

    Thanks to you i can read a bit more again :) and learn of course cause memory stream compression is something new for me.

     

    Kind Regards,

    Michael

    Friday, March 11, 2011 6:37 PM
  • As i said earlier, if the file is too big, you will run into out of memory exception. I got the exception when i try it on executable file over 150MB on system with 960MB RAM but no problem with file less than 150MB. You might be able to compress more size with higher RAM.  Below is sample code, you can test it and see what happen.

    Imports System.IO
    Imports System.IO.Compression
    
     Private p1 As String = "C:\ResourcesFiles\kaymaf.exe"
      Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim f As New FileInfo(p1)
        Dim st As String = "Original Size: " & f.Length.ToString & vbNewLine
        'read the file as byte
        Dim bt() As Byte = IO.File.ReadAllBytes(p1)
        st &= "Byte size before compressed: " & bt.Length.ToString & vbNewLine
        'compress the byte in memory
        Dim comByte() As Byte = Compress(bt)
        st &= "Byte size after compressed: " & comByte.Length.ToString & vbNewLine
        MsgBox(st)
      End Sub
    
    Public Shared Function Compress(ByVal data As Byte()) As Byte()
        Dim ms As New MemoryStream()
        Dim ds As New DeflateStream(ms, CompressionMode.Compress)
        ds.Write(data, 0, data.Length)
        ds.Flush()
        ds.Close()
        Return ms.ToArray()
      End Function
    
    kaymaf

     


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by Michael Huijskens Saturday, March 12, 2011 12:23 AM
    • Unmarked as answer by Michael Huijskens Saturday, March 12, 2011 10:37 AM
    • Proposed as answer by Mike Feng Thursday, March 17, 2011 5:47 AM
    • Marked as answer by Mike Feng Wednesday, March 23, 2011 7:03 AM
    Friday, March 11, 2011 10:00 PM
  • You're not going to save much by compressing in memory (Particularly if you get frequent Out Of Memory errors).The compression algorithms take more time than the file IO.  Compress the file, and if the size isn't reduced enough, delete it.

    Friday, March 11, 2011 10:38 PM
  • thank you KayMaf i'm going to see what i can do with that piece of code :)

    But first i'm going to read a bit more about memory streaming etc in VB.net like i said earlyer i never done it before and i want to know what i'm doing instead of just copy paste your code.

     

    Kind Regards,

    Michael

     

     

    Saturday, March 12, 2011 12:25 AM
  • You're not going to save much by compressing in memory (Particularly if you get frequent Out Of Memory errors).The compression algorithms take more time than the file IO.  Compress the file, and if the size isn't reduced enough, delete it.

    Yes thats true John i tryed the code of Kaymaf but even with 8 GB of RAM so lots of free memory i get a out of memory error over a size of 150 MB.

     

    Kaymaf: maybe i should create a tmp folder when pushed the button compress to that folder delete folder and file after the results are showed.

    Any other idea's ?

     

    Michael

    Saturday, March 12, 2011 10:37 AM
  • Kaymaf: maybe i should create a tmp folder when pushed the button compress to that folder delete folder and file after the results are showed.

    Any other idea's ?

     

    Michael

    Temporary compression will be the best option as you are dealing with big size file.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Saturday, March 12, 2011 11:20 AM
  • KayMaf i tryed already for a couple of hours to find a good way to do this temporary compression.

    I did have a good look at this page http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

    but so far it did not help that much.

    So i did have another look at the code you did provide and tryed to change the compress part from memory streaming to a filestream so far no luck.

     

    Michael

    Saturday, March 12, 2011 2:09 PM
    • Proposed as answer by Mike Feng Thursday, March 17, 2011 5:46 AM
    • Marked as answer by Mike Feng Wednesday, March 23, 2011 7:03 AM
    Saturday, March 12, 2011 3:22 PM
  • Hi Michael,

    Any update?

    How about Kaymaf's suggestion? If it works, you can share your solutions & experience here, it will be very beneficial for other community members who have similar questions.

    Thank you very much.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 17, 2011 3:18 AM
  • Hi Michael,

    I have marked my own and Kaymaf's replies as anwser, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.

     

    Best Regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 7:03 AM
  • Well i'm still bussy with this problem KayMaf did help me out great so far but at moment i still don't have a full working solution if i find it i will post it in the Thread so others can use it.

     

     

    Wednesday, March 23, 2011 1:13 PM