Exemplo n.º 1
0
        // https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples
        public static void ExtractTarByEntry(string tarFileName, string targetDir, bool asciiTranslate)
        {
            using (System.IO.FileStream fsIn = new System.IO.FileStream(tarFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                ICSharpCode.SharpZipLib.Tar.TarInputStream tarIn = new ICSharpCode.SharpZipLib.Tar.TarInputStream(fsIn);
                ICSharpCode.SharpZipLib.Tar.TarEntry       tarEntry;

                while ((tarEntry = tarIn.GetNextEntry()) != null)
                {
                    if (tarEntry.IsDirectory)
                    {
                        System.Console.WriteLine(tarEntry.Name);

                        // ICSharpCode.SharpZipLib.Tar.TarEntry[] entries = tarEntry.GetDirectoryEntries();
                        // System.Console.WriteLine(entries);

                        continue;
                    }
                    // Converts the unix forward slashes in the filenames to windows backslashes
                    string name = tarEntry.Name.Replace('/', System.IO.Path.DirectorySeparatorChar);

                    // Remove any root e.g. '\' because a PathRooted filename defeats Path.Combine
                    if (System.IO.Path.IsPathRooted(name))
                    {
                        name = name.Substring(System.IO.Path.GetPathRoot(name).Length);
                    }

                    // Apply further name transformations here as necessary
                    string outName = System.IO.Path.Combine(targetDir, name);

                    string directoryName = System.IO.Path.GetDirectoryName(outName);

                    // Does nothing if directory exists
                    System.IO.Directory.CreateDirectory(directoryName);

                    System.IO.FileStream outStr = new System.IO.FileStream(outName, System.IO.FileMode.Create);

                    if (asciiTranslate)
                    {
                        CopyWithAsciiTranslate(tarIn, outStr);
                    }
                    else
                    {
                        tarIn.CopyEntryContents(outStr);
                    }

                    outStr.Close();

                    // Set the modification date/time. This approach seems to solve timezone issues.
                    System.DateTime myDt = System.DateTime.SpecifyKind(tarEntry.ModTime, System.DateTimeKind.Utc);
                    System.IO.File.SetLastWriteTime(outName, myDt);
                }

                tarIn.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// tar包解压
        /// </summary>
        /// <param name="strFilePath">tar包路径</param>
        /// <param name="strUnpackDir">解压到的目录</param>
        /// <returns></returns>
        public static bool UnpackTarFiles(string strFilePath, string strUnpackDir)
        {
            try
            {
                if (!File.Exists(strFilePath))
                {
                    return(false);
                }

                strUnpackDir = strUnpackDir.Replace("/", "\\");
                if (!strUnpackDir.EndsWith("\\"))
                {
                    strUnpackDir += "\\";
                }

                if (!Directory.Exists(strUnpackDir))
                {
                    Directory.CreateDirectory(strUnpackDir);
                }

                using (FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (ICSharpCode.SharpZipLib.Tar.TarInputStream s = new ICSharpCode.SharpZipLib.Tar.TarInputStream(fr))
                    {
                        ICSharpCode.SharpZipLib.Tar.TarEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string directoryName = Path.GetDirectoryName(theEntry.Name);
                            string fileName      = Path.GetFileName(theEntry.Name);

                            if (directoryName != String.Empty)
                            {
                                Directory.CreateDirectory(strUnpackDir + directoryName);
                            }

                            if (fileName != String.Empty)
                            {
                                FileStream streamWriter = File.Create(strUnpackDir + theEntry.Name);

                                int    size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                streamWriter.Close();
                            }
                        }
                    }
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        private void _DeCompressTAR(string pathFileTAR, IDTSComponentEvents componentEvents, System.IO.Stream stream)
        {
            bool b = false;

            if (stream == null)
            {
                stream = System.IO.File.OpenRead(pathFileTAR);
            }

            using (ICSharpCode.SharpZipLib.Tar.TarInputStream tar = new ICSharpCode.SharpZipLib.Tar.TarInputStream(stream))
            {
                ICSharpCode.SharpZipLib.Tar.TarEntry te = tar.GetNextEntry();

                while (te != null)
                {
                    string             fn = te.Name.Replace("/", "\\");
                    System.IO.FileInfo fi = null;

                    if ((!System.Text.RegularExpressions.Regex.Match(fn, _fileFilter).Success) || (te.IsDirectory && te.Size == 0))
                    {
                        if (!System.Text.RegularExpressions.Regex.Match(fn, _fileFilter).Success)
                        {
                            componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": file " + fn + " doesn't match regex filter '" + _fileFilter + "'", null, 0, ref b); //  Added information display when regex doesn't match (Updated on 2015-12-30 by Nico_FR75)
                        }

                        te = tar.GetNextEntry();
                        continue;
                    }

                    componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": De-Compress (with '" + _storePaths.ToString() + "') file: " + fn, null, 0, ref b);

                    if (_storePaths == Store_Paths.Absolute_Paths || _storePaths == Store_Paths.Relative_Paths)
                    {
                        //Absolute / Relative Path
                        fi = new System.IO.FileInfo(_folderDest + fn);
                        if (!System.IO.Directory.Exists(fi.DirectoryName))
                        {
                            System.IO.Directory.CreateDirectory(fi.DirectoryName);
                        }
                    }
                    else if (_storePaths == Store_Paths.No_Paths)
                    {
                        //No Path
                        fi = new System.IO.FileInfo(_folderDest + System.IO.Path.GetFileName(fn));
                    }
                    else
                    {
                        throw new Exception("Please select type Store Paths (No_Paths / Relative_Paths / Absolute_Paths).");
                    }

                    using (System.IO.FileStream fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        tar.CopyTo(fs);
                        fs.Flush();
                    }

                    te = tar.GetNextEntry();
                }

                tar.Flush();
                tar.Close();
            }
        }