예제 #1
0
        public void EmptyTar()
        {
            var ms         = new MemoryStream();
            int recordSize = 0;

            using (TarArchive tarOut = TarArchive.CreateOutputTarArchive(ms))
            {
                recordSize = tarOut.RecordSize;
            }

            Assert.IsTrue(ms.GetBuffer().Length > 0, "Archive size must be > zero");
            Assert.Zero(ms.GetBuffer().Length % recordSize, "Archive size must be a multiple of record size");

            var ms2 = new MemoryStream();

            ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms2.Seek(0, SeekOrigin.Begin);

            using (TarArchive tarIn = TarArchive.CreateInputTarArchive(ms2, null))
            {
                entryCount = 0;
                tarIn.ProgressMessageEvent += EntryCounter;
                tarIn.ListContents();
                Assert.AreEqual(0, entryCount, "Expected 0 tar entries");
            }
        }
예제 #2
0
파일: Utility.cs 프로젝트: yply/HomeGenie
        internal static List<string> UncompressTgz(string archiveName, string destinationFolder)
        {
            List<string> extractedFiles = new List<string>();
            try
            {
                Stream inStream = File.OpenRead(archiveName);
                Stream gzipStream = new GZipInputStream(inStream);

                TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
                tarArchive.ProgressMessageEvent += (archive, entry, message) => {
                    extractedFiles.Add(entry.Name);
                };

                tarArchive.ExtractContents(destinationFolder);
                tarArchive.ListContents();
                tarArchive.Close();

                gzipStream.Close();
                inStream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("UnTar error: " + e.Message);
            }

            return extractedFiles;
        }
예제 #3
0
        public static void List()
        {
            TarArchive ta = TarArchive.CreateInputTarArchive(new GZipStream(new FileStream(@"data.bin", FileMode.Open, FileAccess.Read), CompressionMode.Decompress));

            ta.ProgressMessageEvent += MyLister;
            ta.ListContents();
            ta.Close();
        }
        public static void List(String name)
        {
            TarArchive ta = TarArchive.CreateInputTarArchive(new
                                                             FileStream(@name, FileMode.Open, FileAccess.Read));

            ta.ProgressMessageEvent += MyLister;
            ta.ListContents();
            ta.Close();
        }
예제 #5
0
        internal void BrowseFiles(string fullName, CompressedFile fileToReplace)
        {
            this.FullName = fullName;
            string ext = Path.GetExtension(fullName.ToLower());

            if ((ext == EXT_ZIP) || (ext == EXT_JAR))
            {
                using (ZipFile zipFile = new ZipFile(fullName))
                {
                    foreach (ZipEntry zipEntry in zipFile)
                    {
                        if (zipEntry.IsDirectory)
                        {
                            addDirectory(zipEntry);
                        }
                        else if (zipEntry.IsFile)
                        {
                            addFile(zipEntry);
                        }
                    }
                }
            }
            else if ((ext == ".gz") || (ext == ".gzip"))   // tar+gzip
            {
                using (var f = File.OpenRead(fullName))
                    using (Stream inStream = new GZipInputStream(f))
                    {
                        TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);
                        tarArchive.ProgressMessageEvent += new ProgressMessageHandler(tarArchive_ProgressMessageEvent);
                        tarArchive.ListContents();
                    }
            }
            else if ((ext == ".bz2") || (ext == ".bzip2"))   // tar+bz
            {
                using (var f = File.OpenRead(fullName))
                    using (Stream inStream = new BZip2InputStream(f))
                    {
                        TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);
                        tarArchive.ProgressMessageEvent += new ProgressMessageHandler(tarArchive_ProgressMessageEvent);
                        tarArchive.ListContents();
                    }
            }
            else if (ext == ".tar")
            {
                TarArchive tarArchive = TarArchive.CreateInputTarArchive(File.OpenRead(fullName));
                tarArchive.ProgressMessageEvent += new ProgressMessageHandler(tarArchive_ProgressMessageEvent);
                tarArchive.ListContents();
            }
            else if (ext == EXT_RAR)
            {
                openRarFile(fullName);
            }
            copyAdditionalInfo(fileToReplace);
        }
예제 #6
0
        private static List <string> GetFileListFromArchive(string targetFileName)
        {
            List <string> fileList = new List <string>();

            Stream     inStream   = File.OpenRead(targetFileName);
            TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);

            tarArchive.ProgressMessageEvent += delegate(TarArchive archive1, TarEntry entry, string message)
            {
                fileList.Add(entry.Name);
            };
            tarArchive.ListContents();
            return(fileList);
        }
        private static List <string> GetFileListFromArchive(string targetFileName)
        {
            List <string> fileList = new List <string>();

            Stream     inStream   = File.OpenRead(targetFileName);
            TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);

            tarArchive.ProgressMessageEvent += delegate(TarArchive archive1, TarEntry entry, string message)
            {
                // remove base path of all entries - 17 is length of 'package-creation\'
                fileList.Add(entry.Name.Substring(entry.Name.IndexOf("package-creation", StringComparison.Ordinal) + 17));
            };
            tarArchive.ListContents();
            return(fileList);
        }
예제 #8
0
        // check that the tar contains the expected files
        private static void VerifyTar(HashSet <String> expected, MemoryStream outstream)
        {
            MemoryStream instream = new MemoryStream(outstream.ToArray(), false);

            using (TarArchive archive = TarArchive.CreateInputTarArchive(instream))
            {
                HashSet <string> entries = new HashSet <string>();

                archive.ProgressMessageEvent += (ar, entry, message) =>
                {
                    entries.Add(entry.Name);
                };

                archive.ListContents();

                Assert.Equal(expected, entries);
            }
        }
예제 #9
0
        public void EmptyTar()
        {
            MemoryStream ms     = new MemoryStream();
            TarArchive   tarOut = TarArchive.CreateOutputTarArchive(ms);

            tarOut.CloseArchive();

            Assert.IsTrue(ms.GetBuffer().Length > 0, "Archive size must be > zero");
            Assert.AreEqual(ms.GetBuffer().Length % tarOut.RecordSize, 0, "Archive size must be a multiple of record size");

            MemoryStream ms2 = new MemoryStream();

            ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms2.Seek(0, SeekOrigin.Begin);

            TarArchive tarIn = TarArchive.CreateInputTarArchive(ms2);

            entryCount = 0;
            tarIn.ProgressMessageEvent += new ProgressMessageHandler(EntryCounter);
            tarIn.ListContents();
            Assert.AreEqual(0, entryCount, "Expected 0 tar entries");
        }
    /// <summary>
    /// This is the "real" main. The class main() instantiates a tar object
    /// for the application and then calls this method. Process the arguments
    /// and perform the requested operation.
    /// </summary>
    public void InstanceMain(string[] argv)
    {
        TarArchive archive = null;

        int argIdx = this.ProcessArguments(argv);

        if (this.archiveName != null && !this.archiveName.Equals("-"))
        {
            if (operation == Operation.Create)
            {
                string dirName = Path.GetDirectoryName(archiveName);
                if ((dirName.Length > 0) && !Directory.Exists(dirName))
                {
                    Console.Error.WriteLine("Directory for archive doesnt exist");
                    return;
                }
            }
            else
            {
                if (File.Exists(this.archiveName) == false)
                {
                    Console.Error.WriteLine("File does not exist " + this.archiveName);
                    return;
                }
            }
        }

        if (operation == Operation.Create)
        {                              // WRITING
            Stream outStream = Console.OpenStandardOutput();

            if (this.archiveName != null && !this.archiveName.Equals("-"))
            {
                outStream = File.Create(archiveName);
            }

            if (outStream != null)
            {
                switch (this.compression)
                {
                case Compression.Compress:
                    outStream = new DeflaterOutputStream(outStream);
                    break;

                case Compression.Gzip:
                    outStream = new GZipOutputStream(outStream);
                    break;

                case Compression.Bzip2:
                    outStream = new BZip2OutputStream(outStream, 9);
                    break;
                }
                archive = TarArchive.CreateOutputTarArchive(outStream, this.blockingFactor);
            }
        }
        else
        {                                                               // EXTRACTING OR LISTING
            Stream inStream = Console.OpenStandardInput();

            if (this.archiveName != null && !this.archiveName.Equals("-"))
            {
                inStream = File.OpenRead(archiveName);
            }

            if (inStream != null)
            {
                switch (this.compression)
                {
                case Compression.Compress:
                    inStream = new InflaterInputStream(inStream);
                    break;

                case Compression.Gzip:
                    inStream = new GZipInputStream(inStream);
                    break;

                case Compression.Bzip2:
                    inStream = new BZip2InputStream(inStream);
                    break;
                }
                archive = TarArchive.CreateInputTarArchive(inStream, this.blockingFactor);
            }
        }

        if (archive != null)
        {                                               // SET ARCHIVE OPTIONS
            archive.SetKeepOldFiles(this.keepOldFiles);
            archive.AsciiTranslate = this.asciiTranslate;

            archive.SetUserInfo(this.userId, this.userName, this.groupId, this.groupName);
        }

        if (archive == null)
        {
            Console.Error.WriteLine("no processing due to errors");
        }
        else if (operation == Operation.Create)
        {                        // WRITING
            if (verbose)
            {
                archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage);
            }

            for (; argIdx < argv.Length; ++argIdx)
            {
                string[] fileNames = GetFilesForSpec(argv[argIdx]);
                if (fileNames.Length > 0)
                {
                    foreach (string name in fileNames)
                    {
                        TarEntry entry = TarEntry.CreateEntryFromFile(name);
                        archive.WriteEntry(entry, true);
                    }
                }
                else
                {
                    Console.Error.Write("No files for " + argv[argIdx]);
                }
            }
        }
        else if (operation == Operation.List)
        {                   // LISTING
            archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage);
            archive.ListContents();
        }
        else
        {                                                    // EXTRACTING
            string userDir = Environment.CurrentDirectory;
            if (verbose)
            {
                archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage);
            }

            if (userDir != null)
            {
                archive.ExtractContents(userDir);
            }
        }

        if (archive != null)
        {                                   // CLOSE ARCHIVE
            archive.Close();
        }
    }
예제 #11
0
        public override StringDictionary GetProperties(string source)
        {
            if (File.Exists(source))
            {
                string outputFile = GlobalData.OutputFile;//contains the list of files 1 for each line
                switch (Path.GetExtension(source).ToLower())
                {
                case ".rar":
                    try
                    {
                        Unrar unrar = new Unrar(source);
                        unrar.Open(Unrar.OpenMode.List);
                        StreamWriter sw = new StreamWriter(outputFile);     //The outputFile will be Created and opened for writing
                        unrar.WriteFileList(ref sw);
                        sw.Close();
                        unrar.Close();
                        fileProperties["content"] = outputFile;
                    }
                    catch
                    {
                        RemoveFileSpecificKeys();
#if Log
                        Console.WriteLine(source + " => not a valid rar archive");
#endif
                    }
                    break;

                case ".zip":
                    try
                    {
                        FileStream   fs      = File.OpenRead(source);        //Open the archieve file stream
                        ZipFile      zipFile = new ZipFile(fs);              //Create a zip Object from the opened stream
                        StreamWriter sw      = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing
                        foreach (ZipEntry zipEntry in zipFile)
                        {
                            if (zipEntry.IsFile)
                            {
                                sw.WriteLine(zipEntry.Name);
                            }
                        }
                        sw.Close();
                        zipFile.Close();
                        fileProperties["content"] = outputFile;
                        fs.Close();
                    }
                    catch
                    {
                        RemoveFileSpecificKeys();
#if Log
                        Console.WriteLine(source + " => not a valid zip archive");
#endif
                    }
                    break;

                case ".tar":
                    try
                    {
                        FileStream   fs = File.OpenRead(source);
                        TarArchive   ta = TarArchive.CreateInputTarArchive(fs);
                        StreamWriter sw = new StreamWriter(outputFile);     //The outputFile will be Created and opened for writing
                        ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message)
                        {
                            if (!entry.IsDirectory)
                            {
                                sw.WriteLine(entry.Name);
                            }
                        };
                        ta.ListContents();              //List the contents of tar archieve
                        sw.Close();
                        ta.Close();
                        fileProperties["content"] = outputFile;
                        fs.Close();
                    }
                    catch
                    {
                        RemoveFileSpecificKeys();
#if Log
                        Console.WriteLine(source + " => not a valid tar archive");
#endif
                    }
                    break;

                case ".gz":
                    try
                    {
                        FileStream   fs = File.OpenRead(source);
                        TarArchive   ta = TarArchive.CreateInputTarArchive(new GZipInputStream(fs));
                        StreamWriter sw = new StreamWriter(outputFile);     //The outputFile will be Created and opened for writing
                        ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message)
                        {
                            if (!entry.IsDirectory)
                            {
                                sw.WriteLine(entry.Name);
                            }
                        };
                        ta.ListContents();                  //List the contents of gz2 archieve
                        sw.Close();
                        ta.Close();
                        fileProperties["content"] = outputFile;
                        fs.Close();
                    }
                    catch
                    {
                        RemoveFileSpecificKeys();
#if Log
                        Console.WriteLine(source + " => not a valid gz archive");
#endif
                    }
                    break;

                case ".bz2":
                    try
                    {
                        FileStream   fs = File.OpenRead(source);
                        TarArchive   ta = TarArchive.CreateInputTarArchive(new BZip2InputStream(fs));
                        StreamWriter sw = new StreamWriter(outputFile);     //The outputFile will be Created and opened for writing
                        ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message)
                        {
                            if (!entry.IsDirectory)
                            {
                                sw.WriteLine(entry.Name);
                            }
                        };
                        ta.ListContents();              //List the contents of bz2 archieve
                        sw.Close();
                        ta.Close();
                        fileProperties["content"] = outputFile;
                        fs.Close();
                    }
                    catch
                    {
                        RemoveFileSpecificKeys();
#if Log
                        Console.WriteLine(source + " => not a valid bz2 archive");
#endif
                    }
                    break;
                }
                return(base.GetProperties(source));
            }
            else if (Win32Helper.PathExist(source))
            {
                RemoveFileSpecificKeys(); return(base.GetProperties(source));
            }
            return(null);
        }
예제 #12
0
        static int UntarFromResources_Lua(IntPtr L)
        {
            var        uri        = Api.lua_tostring(L, 1);
            var        path       = Api.lua_tostring(L, 2);
            var        unpackPath = Path.Combine(Application.persistentDataPath, path);
            TarArchive archive    = null;

            try
            {
                if (!Directory.Exists(unpackPath))
                {
                    Directory.CreateDirectory(unpackPath);
                }
                var forceUnpack = false;
                if (Api.lua_gettop(L) == 3)
                {
                    forceUnpack = Api.lua_toboolean(L, 3);
                }
                var bytes = ResMgr.LoadBytes(uri);
                var ms    = new MemoryStream(bytes);
                archive = TarArchive.CreateInputTarArchive(ms);
                List <string> names = new List <string>();
                archive.ProgressMessageEvent += (ar, entry, msg) =>
                {
                    names.Add(entry.Name);
                };
                bool shouldUnpack = false;
                if (forceUnpack)
                {
                    shouldUnpack = true;
                }
                else
                {
                    archive.ListContents();
                    for (int i = 0; i < names.Count; ++i)
                    {
                        var p = Path.Combine(unpackPath, names[i]);
                        if (!File.Exists(p))
                        {
                            shouldUnpack = true;
                            break;
                        }
                    }
                    if (shouldUnpack)
                    {
                        archive.Dispose();
                        archive = TarArchive.CreateInputTarArchive(new MemoryStream(bytes));
                    }
                }
                if (shouldUnpack)
                {
                    archive.ExtractContents(unpackPath);
                }
                archive.Dispose();

                Api.lua_createtable(L, names.Count, 0);
                for (int i = 0; i < names.Count; ++i)
                {
                    Api.lua_pushstring(L, names[i]);
                    Api.lua_seti(L, -2, i);
                }
                return(1);
            }
            catch (Exception e)
            {
                if (archive != null)
                {
                    archive.Dispose();
                }
                Api.lua_pushnil(L);
                Api.lua_pushstring(L, e.Message);
                return(2);
            }
        }