Пример #1
0
        /// <summary>
        /// Extracts the file from the gzip archive.
        /// </summary>
        protected override void ExecuteTask()
        {
            try {
                using (GZipInputStream gzs = new GZipInputStream(SrcFile.OpenRead())) {
                    Log(Level.Info, "Expanding '{0}' to '{1}' ({2} bytes).",
                        SrcFile.FullName, DestFile.FullName, gzs.Length);

                    // holds data from src file
                    byte[] data = new byte[8 * 1024];

                    // first read from input to ensure we're dealing with valid
                    // src file before we actually create the dest file
                    int size = gzs.Read(data, 0, data.Length);

                    // write expanded data to dest file
                    using (FileStream fs = new FileStream(DestFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None)) {
                        while (size > 0)
                        {
                            fs.Write(data, 0, size);
                            size = gzs.Read(data, 0, data.Length);
                        }
                        // close output stream
                        fs.Close();
                    }
                    // close input stream
                    gzs.Close();
                }
            } catch (IOException ex) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                       "Failed to expand '{0}' to '{1}'.", SrcFile.FullName,
                                                       DestFile.FullName), Location, ex);

                /* Uncomment when upgrade to next #ziplib release is done
                 * } catch (GZipException ex) {
                 *  throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                 *      "Invalid gzip file '{0}'.", SrcFile.FullName), Location, ex);
                 */
            }
        }
Пример #2
0
        /// <summary>
        /// Extracts the files from the archive.
        /// </summary>
        protected override void ExecuteTask()
        {
            Stream fs       = null;
            Stream instream = null;

            try {
                // ensure archive exists
                if (!SrcFile.Exists)
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           "Tar file '{0}' does not exist.", SrcFile.FullName),
                                             Location);
                }

                fs = SrcFile.OpenRead();

                // wrap inputstream with corresponding compression method
                switch (CompressionMethod)
                {
                case TarCompressionMethod.GZip:
                    instream = new GZipInputStream(fs);
                    break;

                case TarCompressionMethod.BZip2:
                    instream = new BZip2InputStream(fs);
                    break;

                default:
                    instream = fs;
                    break;
                }

                using (TarInputStream s = new TarInputStream(instream)) {
                    Log(Level.Info, "Expanding '{0}' to '{1}'.",
                        SrcFile.FullName, DestinationDirectory.FullName);

                    TarEntry entry;

                    // extract the file or directory entry
                    while ((entry = s.GetNextEntry()) != null)
                    {
                        if (entry.IsDirectory)
                        {
                            ExtractDirectory(s, DestinationDirectory.FullName,
                                             entry.Name, entry.ModTime);
                        }
                        else
                        {
                            ExtractFile(s, DestinationDirectory.FullName,
                                        entry.Name, entry.ModTime, entry.Size);
                        }
                    }
                }
            } catch (IOException ex) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                       "Failed to expand '{0}' to '{1}'.", SrcFile.FullName,
                                                       DestinationDirectory.FullName), Location, ex);
            } catch (TarException ex) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                       "Invalid tar file '{0}'.", SrcFile.FullName), Location, ex);
            } catch (BZip2Exception ex) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                       "Invalid bzip2'd tar file '{0}'.", SrcFile.FullName), Location, ex);
            } catch (GZipException ex) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                       "Invalid gzipped tar file '{0}'.", SrcFile.FullName), Location, ex);
            } finally {
                // close the filestream
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }