Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added progress bar to display the progress status of compressing the file. But upon clicking cancel button of progress bar its throwing exception.

this code is written in GZIPMAiN.cs

VB
case GZipOperationEventArgs.OperationType.Compress: GZip.CompressTPFtoGZip(
                    gzipOperations.TPFfileDir,
                        ((CompressingEventArgs)gzipOperations).CompressTargetDir,
                         (percent, msg) => worker.ReportProgress(percent, msg),
                        () => worker.CancellationPending);
                   break;


this code is written in GZIP.cs

VB
public static void CompressTPFtoGZip(string tpfSrcDir, string targetDir, Action<int, string> progressStatus, Func<bool> cancel)
        {
            try
            {
                int fileCount = 0;
                string[] getFiles = tpfSrcDir.Split(GZipConstants.SPLIT);
                int totalfileCount = getFiles.Length;
                string zipFileName = null;
                foreach (string item in getFiles)
                {
                    FileInfo fileToCompress = new FileInfo(item);
                    FileInfo new1 = new FileInfo(item);
                    var fileInfo = new1.FullName;
                    string fileDetails = fileToCompress.FullName;
                   // int fileCount = 0;
                    using (FileStream originalFileStream = fileToCompress.OpenRead())
                    {
                        if ((File.GetAttributes(fileToCompress.FullName) &
                           FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != GZipConstants.GZEXTENSION)
                        {
                            int length = Convert.ToInt32(originalFileStream.Length);
                            if (!string.IsNullOrEmpty(targetDir))
                            {
                                string getFileName = Path.GetFileName(fileDetails);
                                zipFileName = targetDir + getFileName + GZipConstants.GZEXTENSION;
                                ////System.IO.Directory.CreateDirectory(targetDir); // user provided path
                            }
                            else
                            {
                                zipFileName = fileToCompress.FullName + GZipConstants.GZEXTENSION;
                            }

                            if (File.Exists(zipFileName))
                            {
                                File.Delete(zipFileName);
                            }
                            //// create .gz file and copy the data
                            using (FileStream compressedFileStream = File.Create(zipFileName))
                            {
                                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)
                                    {
                                        countPrg = numRead + countPrg;
                                        try
                                        {
                                            if ((cancel != null) && cancel())
                                            {
                                                break;

                                                System.IO.File.Delete(zipFileName);
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            MessageBox.Show(GZipConstants.CANTCOPYMESS);
                                        }
                                       

                                        if (countPrg != length)
                                        {
                                            int a = length - countPrg;
                                            int b = a / (length / 100);
                                            progressStatus(100 - b, GZipConstants.GZIPFILE + fileToCompress.Name);
                                        }

                                        compressionStream.Write(buffer, GZipConstants.ZERO, numRead);
                                    }
                                }
                            }

                            MessageBox.Show(GZipConstants.DISPLAYFILECREATEDMESSAGE, item);
                        }
                        try
                        {
                            if ((cancel != null) && cancel())
                            {
                                break;
                                System.IO.File.Delete(zipFileName);
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(GZipConstants.CANTCOPYMESS);
                        }
                        
                    }

                    progressStatus((++fileCount * 100) / totalfileCount, GZipConstants.GZIPFILE + fileToCompress.Name);
                }
            }
            catch (Exception)
            {
                MessageBox.Show(GZipConstants.CANTCOPYMESS);
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 10-Nov-14 2:40am    
You need to provide full exception information; you did not even show in what line. But before you post it, use the debugger.
—SA

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