private static void ReadTarEntryData(TarInputStream tarIn, Stream outStream, byte[] buffer = null)
 {
     if (buffer == null)
     {
         buffer = new byte[4096];
     }
     for (int num = tarIn.Read(buffer, 0, buffer.Length); num > 0; num = tarIn.Read(buffer, 0, buffer.Length))
     {
         outStream.Write(buffer, 0, num);
     }
 }
        public static long ReadNextFile(this TarInputStream tarIn, Stream outStream)
        {
            long totalRead = 0;

            byte[] buffer  = new byte[4096];
            bool   isAscii = true;
            bool   cr      = false;

            int numRead  = tarIn.Read(buffer, 0, buffer.Length);
            int maxCheck = Math.Min(200, numRead);

            totalRead += numRead;

            for (int i = 0; i < maxCheck; i++)
            {
                byte b = buffer[i];
                if (b < 8 || (b > 13 && b < 32) || b == 255)
                {
                    isAscii = false;
                    break;
                }
            }

            while (numRead > 0)
            {
                if (isAscii)
                {
                    // Convert LF without CR to CRLF. Handle CRLF split over buffers.
                    for (int i = 0; i < numRead; i++)
                    {
                        byte b = buffer[i];     // assuming plain Ascii and not UTF-16
                        if (b == 10 && !cr)     // LF without CR
                        {
                            outStream.WriteByte(13);
                        }
                        cr = (b == 13);

                        outStream.WriteByte(b);
                    }
                }
                else
                {
                    outStream.Write(buffer, 0, numRead);
                }

                numRead    = tarIn.Read(buffer, 0, buffer.Length);
                totalRead += numRead;
            }

            return(totalRead);
        }
Example #3
0
        public void StreamWithJapaneseName(int length, string encodingName)
        {
            // U+3042 is Japanese Hiragana
            // https://unicode.org/charts/PDF/U3040.pdf
            var entryName = new string((char)0x3042, length);
            var data      = new byte[32];
            var encoding  = Encoding.GetEncoding(encodingName);

            using (var memoryStream = new MemoryStream())
            {
                using (var tarOutput = new TarOutputStream(memoryStream, encoding))
                {
                    var entry = TarEntry.CreateTarEntry(entryName);
                    entry.Size = 32;
                    tarOutput.PutNextEntry(entry);
                    tarOutput.Write(data, 0, data.Length);
                }
                using (var memInput = new MemoryStream(memoryStream.ToArray()))
                    using (var inputStream = new TarInputStream(memInput, encoding))
                    {
                        var buf   = new byte[64];
                        var entry = inputStream.GetNextEntry();
                        Assert.AreEqual(entryName, entry.Name);
                        var bytesread = inputStream.Read(buf, 0, buf.Length);
                        Assert.AreEqual(data.Length, bytesread);
                    }
                File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray());
            }
        }
Example #4
0
        /// <inheritdoc />
        public async Task <byte[]> ReadFileAsync(string id, string filePath, CancellationToken ct = default)
        {
            Stream tarStream;

            try
            {
                tarStream = await this.containers.GetArchiveFromContainerAsync(id, filePath, ct)
                            .ConfigureAwait(false);
            }
            catch (DockerContainerNotFoundException e)
            {
                throw new FileNotFoundException(null, Path.GetFileName(filePath), e);
            }

            using (var tarInputStream = new TarInputStream(tarStream, Encoding.Default))
            {
                tarInputStream.IsStreamOwner = true;

                var entry = tarInputStream.GetNextEntry();

                if (entry.IsDirectory)
                {
                    throw new InvalidOperationException("Can not read from a directory. Use a file instead.");
                }

                var content = new byte[entry.Size];

                // Calling ReadAsync will not work reliably because of some internal buffering in SharpZipLib. This might very well change in future versions of SharpZipLib.
                _ = tarInputStream.Read(content, 0, content.Length);
                return(content);
            }
        }
Example #5
0
        public static UserInfo Get(string name)
        {
            string key = "";

            Image icon = new Bitmap(10, 10);

            DirectoryInfo dir = new DirectoryInfo(SYS.path + "\\User\\" + name);

            using (FileStream stream = (new Temp()).LoadFile(dir.FullName + "\\.UserInfo\\data.tar")) {
                using (TarInputStream file = new TarInputStream(stream)) {
                    TarEntry Epasswd  = file.GetNextEntry();
                    byte[]   BSpasswd = new byte[Epasswd.Size];
                    file.Read(BSpasswd, 0, BSpasswd.Length);
                    key = System.Text.UnicodeEncoding.Unicode.GetString(BSpasswd, 0, BSpasswd.Length);
                    using (FileStream ico = (new Temp()).CreateFile()) {
                        file.GetNextEntry();
                        file.CopyEntryContents(ico);
                        Bitmap b = new Bitmap(ico);
                        icon = b.Clone(new Rectangle(0, 0, b.Width, b.Height), b.PixelFormat);
                        b.Dispose();
                    }
                }
            }


            UserInfo user = new UserInfo(name, key);

            user.Init(icon);
            return(user);
        }
Example #6
0
        /// <summary>
        /// SharpZipLib doesn't account for illegal file names or characters in
        /// file names (e.g. ':' in Windows), so first we stream the tar to a
        /// new tar, sanitizing any of the contained bad file names as we go.
        /// We also need to record the modification times of all the files, so
        /// that we can restore them into the final zip.
        /// </summary>
        public static void SanitizeTarForWindows(string inputTar, string outputTar, Action cancellingDelegate)
        {
            using (var fsIn = File.OpenRead(inputTar))
                using (var inputStream = new TarInputStream(fsIn))
                    using (var fsOut = File.OpenWrite(outputTar))
                        using (var outputStream = new TarOutputStream(fsOut))
                        {
                            TarEntry entry;
                            byte[]   buf       = new byte[8 * 1024];
                            var      usedNames = new Dictionary <string, string>();

                            while ((entry = inputStream.GetNextEntry()) != null)
                            {
                                cancellingDelegate?.Invoke();

                                entry.Name = SanitizeTarName(entry.Name, usedNames);
                                outputStream.PutNextEntry(entry);

                                long bytesToCopy = entry.Size;
                                while (bytesToCopy > 0)
                                {
                                    int bytesRead = inputStream.Read(buf, 0, Math.Min(bytesToCopy > int.MaxValue ? int.MaxValue : (int)bytesToCopy, buf.Length));
                                    outputStream.Write(buf, 0, bytesRead);
                                    bytesToCopy -= bytesRead;
                                }

                                outputStream.CloseEntry();
                            }
                        }
        }
Example #7
0
 // Token: 0x0600001F RID: 31 RVA: 0x00002EB4 File Offset: 0x000010B4
 public static byte[] TarDeCompress(byte[] data, bool isClearData = true)
 {
     byte[] result = null;
     using (MemoryStream memoryStream = new MemoryStream())
     {
         using (MemoryStream memoryStream2 = new MemoryStream(data))
         {
             using (Stream stream = new TarInputStream(memoryStream2))
             {
                 stream.Flush();
                 byte[] array = new byte[2048];
                 int    count;
                 while ((count = stream.Read(array, 0, array.Length)) > 0)
                 {
                     memoryStream.Write(array, 0, count);
                 }
             }
         }
         result = memoryStream.ToArray();
     }
     if (isClearData)
     {
         Array.Clear(data, 0, data.Length);
     }
     return(result);
 }
Example #8
0
 /// <summary>
 /// 解压缩字节数组
 /// </summary>
 /// <param name="data">待解压缩的字节数组</param>
 /// <param name="isClearData">解压缩完成后,是否清除待解压缩字节数组里面的内容</param>
 /// <returns>已解压的字节数组</returns>
 public byte[] TarDeCompress(byte[] data, bool isClearData = true)
 {
     byte[] bytes = null;
     using (MemoryStream o = new MemoryStream())
     {
         using (MemoryStream ms = new MemoryStream(data))
         {
             using (Stream s = new TarInputStream(ms))
             {
                 s.Flush();
                 int    size   = 0;
                 byte[] buffer = new byte[BUFFER_LENGTH];
                 while ((size = s.Read(buffer, 0, buffer.Length)) > 0)
                 {
                     o.Write(buffer, 0, size);
                 }
             }
         }
         bytes = o.ToArray();
     }
     if (isClearData)
     {
         Array.Clear(data, 0, data.Length);
     }
     return(bytes);
 }
Example #9
0
        private void ReadFile()
        {
            var buffer = new byte[1024 * 1024];// more than big enough for all files

            using (var bz2 = new BZip2InputStream(File.Open(_path, FileMode.Open)))
                using (var tar = new TarInputStream(bz2))
                {
                    TarEntry entry;
                    while ((entry = tar.GetNextEntry()) != null)
                    {
                        if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING")
                        {
                            continue;
                        }
                        var readSoFar = 0;
                        while (true)
                        {
                            var bytes = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar);
                            if (bytes == 0)
                            {
                                break;
                            }

                            readSoFar += bytes;
                        }
                        // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff
                        // so we can reads the values properly
                        var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd();
                        _entries.Add(fileText);
                        Interlocked.Increment(ref reads);
                    }
                }
            _entries.Add(null);
        }
Example #10
0
        /// <summary>
        /// 加压缩tar文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="directory"></param>
        /// <returns></returns>
        #region public static bool unTarFile(String path, String directory)
        public static bool unTarFile(String path, String directory)
        {
            bool result = false;

            if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(directory))
            {
                Logger.info(typeof(ZipHelper), "tar file path is empty.");
                return(result);
            }

            if (!File.Exists(path))
            {
                Logger.info(typeof(ZipHelper), "tar file not exist.");
                return(result);
            }

            try
            {
                using (TarInputStream tis = new TarInputStream(File.OpenRead(path)))
                {
                    TarEntry entry = null;
                    while ((entry = tis.GetNextEntry()) != null)
                    {
                        Logger.info(typeof(ZipHelper), String.Format("untar {0}", entry.Name));
                        String parent = Path.GetDirectoryName(entry.Name);
                        String name   = Path.GetFileName(entry.Name);
                        if (parent.Length > 0)
                        {
                            Directory.CreateDirectory(String.Format(@"{0}\{1}", directory, parent));
                        }
                        if (!String.IsNullOrEmpty(name))
                        {
                            using (FileStream fis = File.Create(String.Format(@"{0}\{1}", directory, entry.Name)))
                            {
                                byte[] data = new byte[1024 * 10];
                                while (true)
                                {
                                    int size = tis.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        fis.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                result = true;
            }
            catch (Exception ex)
            {
                Logger.error(typeof(ZipHelper), ex);
            }
            return(result);
        }
Example #11
0
        /// <summary>
        /// tar包解压
        /// </summary>
        /// <param name="strFilePath">tar包路径</param>
        /// <param name="strUnpackDir">解压到的目录</param>
        /// <returns></returns>
        public 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);
                }
                FileStream     fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                TarInputStream s  = new TarInputStream(fr);
                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();
                    }
                }
                s.Close();
                fr.Close();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #12
0
        /// <summary>
        /// Copies the with ASCII translate.
        /// </summary>
        /// <param name="tarIn">
        /// The tar in.
        /// </param>
        /// <param name="outStream">
        /// The out stream.
        /// </param>
        private static void CopyWithAsciiTranslate(TarInputStream tarIn, Stream outStream)
        {
            byte[] buffer  = new byte[4096];
            bool   isAscii = true;
            bool   cr      = false;

            int numRead  = tarIn.Read(buffer, 0, buffer.Length);
            int maxCheck = Math.Min(200, numRead);

            for (int i = 0; i < maxCheck; i++)
            {
                byte b = buffer[i];
                if (b < 8 || (b > 13 && b < 32) || b == 255)
                {
                    isAscii = false;
                    break;
                }
            }

            while (numRead > 0)
            {
                if (isAscii)
                {
                    // Convert LF without CR to CRLF. Handle CRLF split over buffers.
                    for (int i = 0; i < numRead; i++)
                    {
                        byte b = buffer[i]; // assuming plain Ascii and not UTF-16
                        if (b == 10 && !cr) // LF without CR
                        {
                            outStream.WriteByte(13);
                        }

                        cr = b == 13;

                        outStream.WriteByte(b);
                    }
                }
                else
                {
                    outStream.Write(buffer, 0, numRead);
                }

                numRead = tarIn.Read(buffer, 0, buffer.Length);
            }
        }
Example #13
0
        private static Stopwatch ParseDisks(Action <Disk> addToBatch)
        {
            int i      = 0;
            var parser = new Parser();
            var buffer = new byte[1024 * 1024];            // more than big enough for all files

            var sp = Stopwatch.StartNew();

            using (var bz2 = new BZip2InputStream(File.Open(@"D:\Data\freedb-complete-20120101.tar.bz2", FileMode.Open)))
                using (var tar = new TarInputStream(bz2))
                {
                    TarEntry entry;
                    while ((entry = tar.GetNextEntry()) != null)
                    {
                        if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING")
                        {
                            continue;
                        }
                        var readSoFar = 0;
                        while (true)
                        {
                            var read = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar);
                            if (read == 0)
                            {
                                break;
                            }

                            readSoFar += read;
                        }
                        // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff
                        // so we can read the values properly
                        var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd();
                        try
                        {
                            var disk = parser.Parse(fileText);
                            addToBatch(disk);
                            if (i++ % 1000 == 0)
                            {
                                Console.Write("\r{0} {1:#,#}  {2}         ", entry.Name, i, sp.Elapsed);
                            }

                            if (i % 50000 == 0)
                            {
                                return(sp);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine();
                            Console.WriteLine(entry.Name);
                            Console.WriteLine(e);
                            return(sp);
                        }
                    }
                }
            return(sp);
        }
Example #14
0
        public byte[] GetFile(string repository, Layer layer, string path, bool ignoreCase = true)
        {
            var key   = $"static:layer:{layer.Digest}:file:{path}";
            var scope = $"repository:{repository}:pull";

            return(GetCached(scope, key, false, () =>
            {
                var searchParams = path.Patherate();

                using (var stream = GetLayer(repository, layer))
                {
                    var temp = Path.GetTempFileName();
                    try
                    {
                        using (var gunzipped = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                        {
                            using (var gzipStream = new GZipInputStream(stream)) { gzipStream.CopyTo(gunzipped); }
                            gunzipped.Seek(0, SeekOrigin.Begin);
                            using (var tarStream = new TarInputStream(gunzipped))
                            {
                                var entry = tarStream.GetNextEntry();
                                while (entry != null)
                                {
                                    //if we're ignoring case and there are multiple possible matches, we just return the first one we find
                                    if (entry.Name.Matches(searchParams.searchPath, ignoreCase))
                                    {
                                        break;
                                    }
                                    entry = tarStream.GetNextEntry();
                                }

                                if (entry == null || entry.IsDirectory)
                                {
                                    throw new FileNotFoundException();
                                }
                                else
                                {
                                    var bytes = new byte[entry.Size];
                                    tarStream.Read(bytes, 0, (int)entry.Size);
                                    return bytes;
                                }
                            }
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        File.Delete(temp);
                    }
                }
            }));
        }
Example #15
0
        private Stopwatch ParseDisks(Action <Disk> addToBatch)
        {
            int i      = 0;
            var parser = new Parser();
            var buffer = new byte[1024 * 1024];            // more than big enough for all files

            var sp = Stopwatch.StartNew();

            using (var bz2 = new BZip2InputStream(File.Open(dataLocation, FileMode.Open)))
                using (var tar = new TarInputStream(bz2))
                {
                    TarEntry entry;
                    while ((entry = tar.GetNextEntry()) != null)
                    {
                        if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING")
                        {
                            continue;
                        }
                        var readSoFar = 0;
                        while (true)
                        {
                            var read = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar);
                            if (read == 0)
                            {
                                break;
                            }

                            readSoFar += read;
                        }
                        // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff
                        // so we can read the values properly
                        var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd();
                        try
                        {
                            var disk = parser.Parse(fileText);
                            addToBatch(disk);
                            if (i++ % BatchSize == 0)
                            {
                                process.Refresh();
                                MemoryUsage.Add(process.WorkingSet64);
                                logger.Info("\r{0} {1:#,#} {2} ", entry.Name, i, sp.Elapsed);
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Error("");
                            logger.Error(entry.Name);
                            logger.Error(e);
                            return(sp);
                        }
                    }
                }
            return(sp);
        }
Example #16
0
        private static void ParseDisks(BulkInsertOperation insert)
        {
            var parser = new Parser();
            var buffer = new byte[1024 * 1024];// more than big enough for all files

            using (var bz2 = new BZip2InputStream(File.Open(@"I:\Temp\freedb-complete-20150101.tar.bz2", FileMode.Open)))
                using (var tar = new TarInputStream(bz2))
                {
                    int processed = 0;

                    TarEntry entry;
                    while ((entry = tar.GetNextEntry()) != null)
                    {
                        if (processed >= 1000000)
                        {
                            return;
                        }

                        if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING")
                        {
                            continue;
                        }

                        var readSoFar = 0;
                        while (true)
                        {
                            var read = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar);
                            if (read == 0)
                            {
                                break;
                            }

                            readSoFar += read;
                        }

                        // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff
                        // so we can read the values properly
                        var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd();
                        try
                        {
                            var disk = parser.Parse(fileText);
                            insert.Store(disk);

                            processed++;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine();
                            Console.WriteLine(entry.Name);
                            Console.WriteLine(e);
                        }
                    }
                }
        }
Example #17
0
        public static void UnTar(string TarName)
        {
            TarInputStream s = null;

            try
            {
                FileInfo fi = new FileInfo(TarName);

                s = new TarInputStream(File.OpenRead(TarName));
                TarEntry theEntry;

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string FullName = String.Format("{0}\\{1}", fi.DirectoryName, theEntry.Name);
                    string DirName  = Path.GetDirectoryName(FullName);
                    string FileName = Path.GetFileName(FullName);

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

                    if (FileName != String.Empty)
                    {
                        FileStream SW = File.Create(FullName);

                        int    Size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            Size = s.Read(data, 0, data.Length);
                            if (Size > 0)
                            {
                                SW.Write(data, 0, Size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        SW.Close();
                    }
                }
            }
            catch
            {
                //throw;
            }
            finally {
                s.Close();
            }
        }
Example #18
0
        /// <summary>
        /// 解包TAR文件
        /// </summary>
        /// <param name="sourceFilePath">文件的路径</param>
        /// <param name="targetDirectoryPath">解包的输出路径</param>
        public static void DecompressTarFile(string sourceFilePath, string targetDirectoryPath)
        {
            //检查路径拼写
            if (!targetDirectoryPath.EndsWith("\\"))
            {
                targetDirectoryPath += "\\";
            }
            using (TarInputStream tarFile = new TarInputStream(File.OpenRead(sourceFilePath)))
            {
                TarEntry tarEntry = null;
                //获取tar文件信息
                while ((tarEntry = tarFile.GetNextEntry()) != null)
                {
                    string directoryName = "";
                    string pathToTar     = tarEntry.Name;
                    if (!string.IsNullOrEmpty(pathToTar))
                    {
                        //获取文件夹名
                        directoryName = Path.GetDirectoryName(pathToTar) + "\\";
                    }
                    //直接创建文件夹由Directory判断是否创建
                    Directory.CreateDirectory(targetDirectoryPath + directoryName);

                    string fileName = Path.GetFileName(pathToTar);
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (File.Exists(targetDirectoryPath + directoryName + fileName) || !File.Exists(targetDirectoryPath + directoryName + fileName))
                        {
                            using (FileStream outStream = File.Create(targetDirectoryPath + directoryName + fileName))
                            {
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    int bufSize = tarFile.Read(data, 0, data.Length);
                                    if (bufSize > 0)
                                    {
                                        outStream.Write(data, 0, bufSize);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                outStream.Close();
                            }
                        }
                    }
                }
                tarFile.Close();
            }
        }
Example #19
0
        /// <summary>
        /// tar 파일 압축 해제
        /// </summary>
        /// <param name="TarName">tar 파일명</param>
        public static void UnTar(string TarName)
        {
            try
            {
                FileInfo fi = new FileInfo(TarName);

                using (TarInputStream s = new TarInputStream(File.OpenRead(TarName)))
                {
                    TarEntry theEntry;

                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string FullName = string.Format("{0}\\{1}", fi.DirectoryName, theEntry.Name);
                        string DirName  = Path.GetDirectoryName(FullName);
                        string FileName = Path.GetFileName(FullName);

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

                        if (FileName != string.Empty)
                        {
                            FileStream SW = File.Create(FullName);

                            int    Size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                Size = s.Read(data, 0, data.Length);
                                if (Size > 0)
                                {
                                    SW.Write(data, 0, Size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            SW.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new AggregateException("FileCompression.UnTar : Fail untar", e);
            }
        }
Example #20
0
        public override void ExtractCurrentFile(Stream extractedFileContents, Action cancellingDelegate)
        {
            if (IsDirectory())
            {
                return;
            }

            byte[] buffer = new byte[32768];
            int    count;

            while ((count = tarStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                cancellingDelegate?.Invoke();
                extractedFileContents.Write(buffer, 0, count);
            }
        }
        // TODO: Create a class Archive.cs and do all the archiving stuff there! This is just copy and paste crap
        public static ArrayList UncompressTarFile(string Filename, string To, Gtk.ProgressBar bar)
        {
            ArrayList entries = new ArrayList();

            try{
                TarInputStream tarIn = new TarInputStream(File.OpenRead(Filename));
                TarEntry       entry;
                while ((entry = tarIn.GetNextEntry()) != null)
                {
                    string savepath = Path.GetDirectoryName(To + entry.Name);
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                        //Console.WriteLine(Path.GetDirectoryName(entry.Name));
                    }
                    entries.Add(Path.GetDirectoryName(entry.Name));
                    if (!entry.IsDirectory)
                    {
                        FileStream streamWriter = File.Create(To + entry.Name);
                        long       size         = entry.Size;
                        byte[]     data         = new byte[size];
                        while (true)
                        {
                            size = tarIn.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, (int)size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                Console.WriteLine("Deflating the tar file done!");
                return(entries);
            }
            catch (Exception e) {
                Console.WriteLine("An exception occured while deflating the tar file: " + e.Message);
                return(entries);
            }
        }
Example #22
0
 /// <summary>
 /// 解包Tar文件
 /// </summary>
 /// <param name="file"></param>
 /// <param name="path"></param>
 /// <param name="fcp"></param>
 public static void TarDeCompress(string file, string path, Encoding encod, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         if (!Directory.Exists(path))
         {
             Directory.CreateDirectory(path);
         }
         using (var tar = new TarInputStream(File.OpenRead(file), encod))
         {
             TarEntry entry;
             while (null != (entry = tar.GetNextEntry()))
             {
                 if (string.Empty != entry.Name)
                 {
                     if (entry.IsDirectory)
                     {
                         Directory.CreateDirectory(Path.Combine(path, entry.Name));                   //目录
                     }
                     else
                     {
                         var name = Path.Combine(path, entry.Name);
                         using (var fs = File.OpenWrite(name))//文件
                         {
                             var buffer = new byte[_buffer];
                             int i;
                             while ((i = tar.Read(buffer, 0, buffer.Length)) > 0)
                             {
                                 fs.Write(buffer, 0, i);
                                 fcp?.BeginInvoke(name, entry.Size, fs.Position, null, null);
                             }
                         }//end open
                         new FileInfo(name).LastWriteTimeUtc = entry.ModTime;
                     }
                 }
             }
         }//end zip
     }
     else
     {
         throw new Exception("文件不存在!");
     }
 }
Example #23
0
        private static string tarStreamString(TarInputStream stream, TarEntry entry)
        {
            // Read each file into a buffer.
            int buffer_size;

            try
            {
                buffer_size = Convert.ToInt32(entry.Size);
            }
            catch (OverflowException)
            {
                log.ErrorFormat("Error processing {0}: Metadata size too large.", entry.Name);
                return(null);
            }

            byte[] buffer = new byte[buffer_size];

            stream.Read(buffer, 0, buffer_size);

            // Convert the buffer data to a string.
            return(Encoding.ASCII.GetString(buffer));
        }
Example #24
0
        /// <summary>
        /// Updates the supplied registry from the supplied zip file.
        /// This will *clear* the registry of available modules first.
        /// This does not *save* the registry. For that, you probably want Repo.Update
        /// </summary>
        internal static void UpdateRegistryFromTarGz(string path, Registry registry)
        {
            log.DebugFormat("Starting registry update from tar.gz file: \"{0}\".", path);

            // Open the gzip'ed file.
            using (Stream inputStream = File.OpenRead(path))
            {
                // Create a gzip stream.
                using (GZipInputStream gzipStream = new GZipInputStream(inputStream))
                {
                    // Create a handle for the tar stream.
                    using (TarInputStream tarStream = new TarInputStream(gzipStream))
                    {
                        // Walk the archive, looking for .ckan files.
                        const string filter = @"\.ckan$";

                        while (true)
                        {
                            TarEntry entry = tarStream.GetNextEntry();

                            // Check for EOF.
                            if (entry == null)
                            {
                                break;
                            }

                            string filename = entry.Name;

                            // Skip things we don't want.
                            if (!Regex.IsMatch(filename, filter))
                            {
                                log.DebugFormat("Skipping archive entry {0}", filename);
                                continue;
                            }

                            log.DebugFormat("Reading CKAN data from {0}", filename);

                            // Read each file into a buffer.
                            int buffer_size;

                            try
                            {
                                buffer_size = Convert.ToInt32(entry.Size);
                            }
                            catch (OverflowException)
                            {
                                log.ErrorFormat("Error processing {0}: Metadata size too large.", entry.Name);
                                continue;
                            }

                            byte[] buffer = new byte[buffer_size];

                            tarStream.Read(buffer, 0, buffer_size);

                            // Convert the buffer data to a string.
                            string metadata_json = Encoding.ASCII.GetString(buffer);

                            ProcessRegistryMetadataFromJSON(metadata_json, registry, filename);
                        }
                    }
                }
            }
        }
Example #25
0
        /// <summary>
        /// 解压缩tar文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="directory"></param>
        /// <returns></returns>
        #region public static bool unTarFile(String path, String directory)
        public static bool unTarFile(String path, String directory)
        {
            //标记结果
            bool result = false;

            //判断传入参数是否合法
            if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(directory))
            {
                Logger.info(typeof(ZipHelper), "tar file path is empty.");
                return(result);
            }
            //判断文件是否存在
            if (!File.Exists(path))
            {
                Logger.info(typeof(ZipHelper), "tar file not exist.");
                return(result);
            }
            //开始解压
            try
            {
                //获取文件流
                using (TarInputStream tis = new TarInputStream(File.OpenRead(path)))
                {
                    //记录文件流实体
                    TarEntry entry = null;
                    //循环遍历文件流
                    while ((entry = tis.GetNextEntry()) != null)
                    {
                        Logger.info(typeof(ZipHelper), String.Format("untar {0}", entry.Name));
                        //获取文件目录
                        String parent = Path.GetDirectoryName(entry.Name);
                        //获取文件名称
                        String name = Path.GetFileName(entry.Name);
                        if (parent.Length > 0)//判断目录存在,在本地文件系统创建对应文件夹
                        {
                            Directory.CreateDirectory(String.Format(@"{0}\{1}", directory, parent));
                        }
                        if (!String.IsNullOrEmpty(name))//判断文件存在
                        {
                            //创建文件
                            using (FileStream fis = File.Create(String.Format(@"{0}\{1}", directory, entry.Name)))
                            {
                                //写入文件
                                byte[] data = new byte[1024 * 10];
                                while (true)
                                {
                                    int size = tis.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        fis.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                result = true;
            }
            catch (Exception ex)
            {
                Logger.error(typeof(ZipHelper), ex);
            }
            return(result);
        }
Example #26
0
        static public string unTarFile(string TargetFile, string fileDir)
        {
            string rootFile = "";

            try
            {
                //读取压缩文件(zip文件),准备解压缩
                TarInputStream s = new TarInputStream(File.OpenRead(TargetFile.Trim()));
                TarEntry       theEntry;
                //解压出来的文件保存的路径

                string rootDir = " ";
                //根目录下的第一个子文件夹的名称
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string temp = theEntry.Name.PathFilter();
                    rootDir = Path.GetDirectoryName(temp);
                    //得到根目录下的第一级子文件夹的名称
                    if (rootDir.IndexOf("\\") >= 0)
                    {
                        rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                    }
                    string dir = Path.GetDirectoryName(temp);
                    //根目录下的第一级子文件夹的下的文件夹的名称
                    string fileName = Path.GetFileName(temp);
                    //根目录下的文件名称
                    if (dir != " " && dir != "")
                    //创建根目录下的子文件夹,不限制级别
                    {
                        if (!Directory.Exists(fileDir + "\\" + dir))
                        {
                            //在指定的路径创建文件夹
                            Directory.CreateDirectory(fileDir + "\\" + dir);
                        }
                    }

                    //以下为解压缩zip文件的基本步骤
                    //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
                    if (fileName != String.Empty)
                    {
                        FileStream streamWriter = File.Create(fileDir + "\\" + temp);

                        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();
                    }
                }
                s.Close();

                return(rootFile);
            }

            catch (Exception ex)
            {
                return("1; " + ex.Message);
            }
        }
Example #27
0
 public int OnExecute(IConsole console)
 {
     try
     {
         var enc     = Util.GetEncodingFromName(FileNameEncoding, Encoding.UTF8);
         var matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
         if (Includes != null && Includes.Length != 0)
         {
             matcher.AddIncludePatterns(Includes);
         }
         else
         {
             matcher.AddInclude("**/*");
         }
         if (Excludes != null && Excludes.Length != 0)
         {
             matcher.AddExcludePatterns(Excludes);
         }
         var outdir = string.IsNullOrEmpty(OutputDirectory) ? Directory.GetCurrentDirectory() : OutputDirectory;
         using (var istm = TarUtil.GetCompressionStream(Util.OpenInputStream(InputPath), CompressionFormat, TarStreamDirection.Input))
             using (var tstm = new TarInputStream(istm, enc))
             {
                 while (true)
                 {
                     var entry = tstm.GetNextEntry();
                     if (entry == null)
                     {
                         break;
                     }
                     var m = matcher.Match(entry.Name);
                     if (!m.HasMatches)
                     {
                         continue;
                     }
                     if (ListOnly)
                     {
                         console.WriteLine($"{entry.Name}");
                         continue;
                     }
                     var entryKey = Util.ReplaceRegexString(entry.Name, ReplaceFrom, ReplaceTo);
                     if (entry.IsDirectory)
                     {
                         var destdir = Path.Combine(outdir, entryKey);
                         if (!Directory.Exists(destdir))
                         {
                             Directory.CreateDirectory(destdir);
                         }
                     }
                     else
                     {
                         var destfi = new FileInfo(Path.Combine(outdir, entryKey));
                         console.Error.WriteLine($"extracting {entry.Name} to {destfi.FullName}");
                         if (!destfi.Directory.Exists)
                         {
                             destfi.Directory.Create();
                         }
                         using (var deststm = File.Create(destfi.FullName))
                         {
                             if (entry.Size != 0)
                             {
                                 var buf = new byte[4096];
                                 while (true)
                                 {
                                     var bytesread = tstm.Read(buf, 0, buf.Length);
                                     if (bytesread == 0)
                                     {
                                         break;
                                     }
                                     deststm.Write(buf, 0, bytesread);
                                 }
                             }
                         }
                         destfi.LastWriteTime = entry.ModTime;
                     }
                 }
             }
         return(0);
     }
     catch (Exception e)
     {
         console.Error.WriteLine($"failed to decompressing tar archive:{e}");
         return(1);
     }
 }
Example #28
0
        /// <summary>
        /// Metoda, ki se pokliče, ko kliknemo na gumb Razširi. Odpre saveFileDialog in prebere kam bomo datoteko
        /// shranili.
        /// OPOMBA: metoda še ne deluje 100% pravilno.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExtract_Click(object sender, EventArgs e)
        {
            filesList = new StringCollection();

            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fileExplorer.SelectedItems.Count > 0 && fbd.ShowDialog() == DialogResult.OK)
            {
                // Pridobi shranjevalno pot
                string savePath = fbd.SelectedPath;

                foreach (ListViewItem item in fileExplorer.SelectedItems)
                {
                    string file = item.SubItems[0].Text;

                    // Preveri katera oblika datoteke je: ZIP, TAR, GZIP ali TAR.BZ2
                    if (Path.GetExtension(txtPath.Text) == ".zip")
                    {
                        ZipFile  zip   = Ionic.Zip.ZipFile.Read(txtPath.Text);
                        ZipEntry entry = zip[file];
                        if (zip[file].UsesEncryption == true)
                        {
                            PasswordPrompt passWin = new PasswordPrompt();
                            passWin.ShowDialog();

                            zip.Password = passWin.pass;
                            try
                            {
                                entry.ExtractWithPassword(savePath, passWin.pass);
                            }
                            catch (BadPasswordException)
                            {
                                if (MessageBox.Show("Napačno geslo", "Napaka", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                                {
                                    passWin.ShowDialog();
                                }
                            }
                        }

                        string enExists = savePath + @"\" + entry.FileName;

                        if (File.Exists(enExists))
                        {
                            if (MessageBox.Show("Datoteka " + file + " že obstaja. Ali jo želite zamenjati?", "Datoteka obstaja", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                            {
                                entry.Extract(savePath, ExtractExistingFileAction.OverwriteSilently);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            entry.Extract(savePath);
                        }
                    }

                    else if (Path.GetExtension(txtPath.Text) == ".tar")
                    {
                        byte[]         outBuffer = new byte[4096];
                        TarInputStream tar       = new TarInputStream(new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read));
                        TarEntry       curEntry  = tar.GetNextEntry();
                        while (curEntry != null)
                        {
                            if (curEntry.Name == file)
                            {
                                FileStream   fs = new FileStream(savePath + @"\" + curEntry.Name, FileMode.Create, FileAccess.Write);
                                BinaryWriter bw = new BinaryWriter(fs);
                                tar.Read(outBuffer, 0, (int)curEntry.Size);
                                bw.Write(outBuffer, 0, outBuffer.Length);
                                bw.Close();
                            }
                            curEntry = tar.GetNextEntry();
                        }
                        tar.Close();
                    }

                    else if (Path.GetExtension(txtPath.Text) == ".bz2")
                    {
                        Stream           str      = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read);
                        BZip2InputStream bzStr    = new BZip2InputStream(str);
                        TarInputStream   tar      = new TarInputStream(bzStr);
                        TarEntry         curEntry = tar.GetNextEntry();
                        while (curEntry != null)
                        {
                            if (curEntry.Name == file)
                            {
                                byte[]       outBuffer = new byte[curEntry.Size];
                                FileStream   fs        = new FileStream(savePath + @"\" + curEntry.Name, FileMode.Create, FileAccess.Write);
                                BinaryWriter bw        = new BinaryWriter(fs);
                                tar.Read(outBuffer, 0, (int)curEntry.Size);
                                bw.Write(outBuffer, 0, outBuffer.Length);
                                bw.Close();
                            }
                            curEntry = tar.GetNextEntry();
                        }
                        tar.Close();
                    }

                    else if (Path.GetExtension(txtPath.Text) == ".tgz")
                    {
                        Stream          str   = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read);
                        GZipInputStream gzStr = new GZipInputStream(str);
                        TarInputStream  tar   = new TarInputStream(gzStr);

                        TarEntry curEntry = tar.GetNextEntry();
                        while (curEntry != null)
                        {
                            if (curEntry.Name == file)
                            {
                                byte[]       outBuffer = new byte[curEntry.Size];
                                FileStream   fs        = new FileStream(savePath + @"\" + curEntry.Name, FileMode.Create, FileAccess.Write);
                                BinaryWriter bw        = new BinaryWriter(fs);
                                tar.Read(outBuffer, 0, (int)curEntry.Size);
                                bw.Write(outBuffer, 0, outBuffer.Length);
                                bw.Close();
                            }
                            curEntry = tar.GetNextEntry();
                        }
                        tar.Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("Nobena datoteka ni bila izbrana. Prosimo izberite datoteke.", "Napaka", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }