Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
FileInfo fileToCompress = fileInfo;
                    using (FileStream originalFileStream = fileToCompress.OpenRead())
                    {
                        if ((File.GetAttributes(fileToCompress.FullName) &
                           FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != GZipConstants.GZExtension)
                        {
                           int length=Convert.ToInt32(originalFileStream.Length);
                            //string gzFileName = fileToCompress.FullName + GZipConstants.GZExtension;
                            string gzFileName;
                            if (!String.IsNullOrEmpty(targetDir))
                            {
                                string fileDetails = fileToCompress.FullName;
                                string getFileName = Path.GetFileName(fileDetails);
                                gzFileName = targetDir + getFileName + GZipConstants.GZExtension;
                                disp = getFileName;
                                //System.IO.Directory.CreateDirectory(targetDir + getFileName); // user provided path
                            }
                            else
                            {
                                string fileDetails = fileToCompress.FullName;
                                string getFileName = Path.GetFileName(fileDetails);
                                disp = getFileName;
                                gzFileName = fileToCompress.FullName + GZipConstants.GZExtension;
                            }
                            if (File.Exists(gzFileName))
                            {
                                File.Delete(gzFileName);
                            }
                            //int nPercentToAdvance = (100 / filesTPF.Length);
                            // create .gz file and copy the data
                            using (FileStream compressedFileStream = new FileStream(gzFileName, FileMode.Create, FileAccess.Write))
                            {
                                using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                                {
                                    // originalFileStream.CopyTo(compressionStream); // read all the bytes from the current stream  and writes them to destination stream

                                    // Copy the source file into the compression stream.
                                    byte[] buffer = new byte[GZipConstants.GZBufferSize];
                                    int numRead;
                                    int countPrg = 0;
                                    while ((numRead = originalFileStream.Read(buffer, 0, buffer.Length)) != GZipConstants.ZERO)
                                    {
                                        if ((cancel!=null)&&cancel())
                                        {
                                            break;
                                        }
                                        compressionStream.Write(buffer, GZipConstants.ZERO, numRead);
                                        countPrg = numRead + countPrg;
                                        if (countPrg==length)
                                        {
                                            progressStatus((countPrg * 100) / length, GZipConstants.GZipFile + fileToCompress.Name);
                                        }

                                    }
                            
                                }
                            }

                        }
                    }
Posted
Updated 30-Oct-14 23:07pm
v2
Comments
Alex Sprint 7-Nov-14 2:23am    
int a = length - countPrg;
int b = a / (length / 100);
progressStatus(100 - b, GZipConstants.GZIPFILE + fileToCompress.Name);
Alex Sprint 7-Nov-14 2:24am    
this is working fine for large size files (file size >1MB)

1 solution

If there are any errors running your code: where do they occur in the code ?

Have you tried setting a break-point and single-stepping through your code with F11 and examining the changing values of variables ?

If there are no errors:

Modify the place where you (apparently) update the progress bar like this:
C#
if (countPrg==length)
{
    Console.WriteLine("countPrg: {0} length: {1}", countPrg, length);

    progressStatus((countPrg * 100) / length, GZipConstants.GZipFile + fileToCompress.Name);
}
Assuming your app runs without errors and actually does something, if you run this code, and then examine the 'Output window in Visual Studio, you should be able to diagnose how many times the code to update the progress bar has been called. I suspect it's being called only once.
 
Share this answer
 
Comments
Alex Sprint 3-Nov-14 5:56am    
Thanks for replying to the question. Now the progress bar is working fine.
Alex Sprint 6-Nov-14 4:42am    
If the size of "length" is of large size (ex: 73 MB) then the progress bar status is not getting displayed. As the count starts from 4096 and gets incremented in the loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900