Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

how to compress file! RSS

22 replies

Last post Jul 01, 2009 02:56 PM by SaurabhAgg

(0)
  • joji777

    joji777

    Member

    245 Points

    343 Posts

    Re: how to compress file!

    Jun 17, 2009 04:25 AM | LINK

    Hi Mog Liang,

    Thanks for sharing the code, yes it works. However I found following issues, can you please help me out for these issues as well:

    1) I used your given code and it took 2 minutes 25 seconds to compress an audio file size 8.22 Mb, only addition in my code was I set zipoutputstream.SetLevel(9) , I wanted to make maximum compressed file.

    2) In silvelright after user select a file in open file dilaouge it keep the dilaouge showing while compression routine is running, it shows kind of stuck, how can I update the look and give message to user "compressing...." i tried but as soon compression code starts it stucks until compression is done.

    3) is there anyway to get a compressed stream in byte array, wihtout reading it again from stored place ?

     Regards

     

  • Mog Liang - MSFT

    Mog Liang -...

    All-Star

    21645 Points

    2132 Posts

    Microsoft

    Re: how to compress file!

    Jun 17, 2009 06:37 AM | LINK

    For 1st question, based on my test,  write isolatedstorage file cost up to 90% time, compress and decompress only cost about 7% time, so if you use memorystream instead of isolatedstoragefilestream, the process will speed up 10 times

    For second question, try use BackgroundWorker to run compression on other thread.

    my code.  

            FileInfo _fileinfo;
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var ofd1 = new OpenFileDialog();
                var result = ofd1.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    tb1.Text = "Compressing...";
                    _fileinfo = ofd1.File;
    
                    BackgroundWorker bw = new BackgroundWorker();
                    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                    bw.RunWorkerAsync();
                }
            }
    
            void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                //tb1.Text = "Compression complete.";
            }
    
            void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                DateTime starttime = DateTime.Now;
                string timestring="";
    
                //read file to byte[]
                Stream fileStream = _fileinfo.OpenRead();
                var filebuffer = new byte[(int)fileStream.Length];
                fileStream.Read(filebuffer, 0, (int)fileStream.Length);
                fileStream.Close();
                fileStream.Dispose();
    
                timestring += "read file stream: " + (DateTime.Now - starttime).TotalMilliseconds;
                starttime = DateTime.Now;
    
                //create isolatedstorage file
                var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();
                var filestream2 = isolatedStorageFile.CreateFile(_fileinfo.Name + ".zip");
    
                timestring += "\ncreate isolatedstorage file: " + (DateTime.Now - starttime).TotalMilliseconds;
                starttime = DateTime.Now;
    
                //zip
                var zs = new ZipOutputStream(filestream2);
                var entry = new ZipEntry(_fileinfo.Name);
                zs.PutNextEntry(entry);
                zs.Write(filebuffer, 0, filebuffer.Length);
                zs.CloseEntry();
                zs.Close();
                filestream2.Close();
                filestream2.Dispose();
    
                timestring += "\nzip and write isolatedstoreage file: " + (DateTime.Now - starttime).TotalMilliseconds;
                starttime = DateTime.Now;
    
                //get isolatedstorage file
                var filestream3 = isolatedStorageFile.OpenFile(_fileinfo.Name + ".zip", FileMode.Open);
    
                //unzip
                byte[] buff1 = new byte[1024];
                var memstream1 = new MemoryStream();
                var zis = new ZipInputStream(filestream3);
                zis.GetNextEntry();
                int readlength = 0;
                while (true)
                {
                    readlength = zis.Read(buff1, 0, buff1.Length);
                    memstream1.Write(buff1, 0, readlength);
                    if (readlength <= 0)
                        break;
                }
    
                timestring += "\nunzip file: " + (DateTime.Now - starttime).TotalMilliseconds;
                starttime = DateTime.Now;
    
                //display.
                //use Dispather switch to UI thread.
                Dispatcher.BeginInvoke(delegate
                {
                    var bitmap1 = new BitmapImage();
                    bitmap1.SetSource(memstream1);
                    img1.Source = bitmap1;
                    tb1.Text = timestring;
                });
            }  

    For 3rd question, you can use MemoryStream.

    Mog Liang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: how to compress file!

    Jun 17, 2009 07:49 AM | LINK

    Is this code built on SL2 or SL3?  A while ago I reported an IsolatedStorage write performance bug, and it was mentioned that this will be fixed in SL3, although I haven't tested it as I dealt with it using the "workaround" already.  The zip package may very well exercise the same performance problem.

    See this thread http://silverlight.net/forums/p/75315/180892.aspx

    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!
  • joji777

    joji777

    Member

    245 Points

    343 Posts

    Re: how to compress file!

    Jun 17, 2009 01:11 PM | LINK

    many thanks for code samples and guidance, I just tried it with memory stream and it still takes same amount of time to zip. May be i am doing something wrong, can you please update your snippet doing it with memory stream, that will makes me clear if i am suing memory stream in right way or not.

    Best regards

  • Mog Liang - MSFT

    Mog Liang -...

    All-Star

    21645 Points

    2132 Posts

    Microsoft

    Re: how to compress file!

    Jun 18, 2009 02:29 AM | LINK

    my code, compress 6MB wma file spend 2 seconds

        private MemoryStream CompressFile(FileInfo inputfile)
        {
            FileStream inputstream = inputfile.OpenRead();
            MemoryStream stream1 = new MemoryStream();
            ZipOutputStream stream2 = new ZipOutputStream(stream1);
    
            //read file
            byte[] buffer1 = new byte[(int)inputstream.Length];
            inputstream.Read(buffer1, 0, (int) inputstream.Length);
            inputstream.Close();
            inputstream.Dispose();
    
            //compress
            ZipEntry entry = new ZipEntry(inputfile.Name);
            stream2.PutNextEntry(entry);
            stream2.Write(buffer1, 0, buffer1.Length);
            stream2.CloseEntry();
            stream1.Flush();
           
            return stream1;
        }
     

    Mog Liang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: how to compress file!

    Jun 18, 2009 02:33 AM | LINK

    That's way too long to zip.  Perhaps you wanna look at ZipForge which is another zip package.  I have no issue with it.  Perhaps more efficient than SharpZipLib?
    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!
  • joji777

    joji777

    Member

    245 Points

    343 Posts

    Re: how to compress file!

    Jun 18, 2009 12:13 PM | LINK

    one critical issue i am unable to figure out so far is follwoing exception. my requirments are like, I zip a file in silverlight component and later i have to read that file for my java applet. when we try to open file in java following exception occur.

    Exception in thread "main" java.util.zip.ZipException: invalid entry size (expected 4294967295 but got 8628524 bytes)

        at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:373)

        at java.util.zip.ZipInputStream.read(ZipInputStream.java:141)

        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)

     

    why header is incorrect ??

  • joji777

    joji777

    Member

    245 Points

    343 Posts

    Re: how to compress file!

    Jun 18, 2009 12:56 PM | LINK

    in above mentioned code if you add following 2 lines, you will not get exception if you trying to unzip file in java

    zipEntry.CompressionMethod = CompressionMethod.Deflated;

    zipEntry.Size = actualfilebufer.Length;

     

    Regards

  • SaurabhAgg

    SaurabhAgg

    Member

    6 Points

    3 Posts

    Re: Re: how to compress file!

    Jul 01, 2009 01:38 PM | LINK

    @Mog Liang

    I couldn't find any ZipOutputStream class for Silverlight, is this from vjslib.dll ?

    If yes, then is there any way we can create a zip of a folder in Silverlight ?

     

    Thanks.

     

    -Saurabh

  • joji777

    joji777

    Member

    245 Points

    343 Posts

    Re: Re: how to compress file!

    Jul 01, 2009 01:47 PM | LINK

    have a look on this thread, hope it will help

     http://silverlight.net/forums/t/21392.aspx