Example #1
0
        /// <summary>
        /// Get/set the value associated with this index as an array of byte.
        /// Note : The change made using the indexer setter, won't be serialized until Update() is called
        /// </summary>
        /// <value></value>
        byte[] IArchiveItemsAdapter.this[string key]
        {
            get
            {
                byte[] retVal = null;
                if (_archiveHash.Contains(key) == false)
                {
                    // @ review : return null or throw ?
                    throw new CompressionException("Key '" + key + "' cannot be found in this archive");
                }

                string path = (string)_archiveHash[key];
                if (path == null || path.Trim() == string.Empty)
                {
                    // @ review : return null or throw ?
                    throw new ApplicationException("The path associated with key '" + key + "' is empty/null");
                }
#if CLR_VERSION_BELOW_2
                FileStream fs = null;
                try
                {
                    fs = File.OpenRead(path);
                    fs.Read(retVal, 0, (int)fs.Length);
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
#else
                retVal = File.ReadAllBytes(path);
#endif

                return(retVal);
            }
            set
            {
                if (value == null || value.Length == 0)
                {
                    throw new ArgumentException("cannot pass a null or empty value");
                }
                Archiver fileOwner = this;
                if (_archiveHash.Contains(key))
                {
                    fileOwner = RemoveItem(key);
                }

                fileOwner.AddBuffer(key, value);
            }
        }
Example #2
0
        /// <summary>
        /// Load an Archive and extract the files
        /// </summary>
        /// <param name="archiverName">The name of the archive to open</param>
        /// <returns>An instance of the Archiver object</returns>
        static public Archiver Load(string archiverName)
        {
            Archiver retVal = null;

            if (archiverName == null || archiverName.Trim() == string.Empty)
            {
                throw new ArgumentNullException("archiverName", "An invalid value was passed in as parameter (null / empty / whitespace)");
            }
            archiverName = TranslateEnvironmentVariable(archiverName);
            if (File.Exists(archiverName) == false)
            {
                throw new FileNotFoundException("the specified file was not found", archiverName);
            }

            retVal = new Archiver(archiverName);
            XmlDocument xmlDoc = new XmlDocument();

            // Get its description file
            retVal.CreateDirectory(retVal._archivePath);
            retVal._cabInfo.ExtractAll(retVal._archivePath, true, null, null);
            xmlDoc.Load(Path.Combine(retVal._archivePath, retVal._cabInfo.Name) + XMLEXTENSION);

            // Populate _files arraylist
            XmlNodeList nodeListFiles = xmlDoc.SelectNodes("*/" + ENTRY);

            foreach (XmlNode node in nodeListFiles)
            {
                FileEntryDescriptor fileDesc = new FileEntryDescriptor(Path.Combine(retVal._archivePath, node.Attributes[NAME].Value), node.Attributes[KEY].Value);
                fileDesc.Root = retVal;
                retVal._files.Add(fileDesc);
                retVal._archiveHash.Add(fileDesc.Index, fileDesc.FullName);
            }

            // Populate _subArchive arraylist
            XmlNodeList nodeListArchive = xmlDoc.SelectNodes("*/" + ARCHIVE);

            foreach (XmlNode node in nodeListArchive)
            {
                Archiver subArchive = Archiver.Load(Path.Combine(retVal._archivePath, node.Attributes[NAME].Value));
                retVal._subArchives.Add(subArchive);
                // copy hash of sub archive in this hash
                IDictionaryEnumerator iter = subArchive._archiveHash.GetEnumerator();
                while (iter.MoveNext())
                {
                    retVal._archiveHash.Add(iter.Key, iter.Value);
                }
            }

            retVal.CheckArchiveCrc();

            return(retVal);
        }
Example #3
0
        private void AddFileInternal(FileEntryDescriptor fileEntry)
        {
            // Check if index already exist
            if (_archiveHash.Contains(fileEntry.Index))
            {
                throw new DuplicateNameException("Index '" + fileEntry.Index + "'  (existing 1 : '" + _archiveHash[fileEntry.Index].ToString() + "' file being added : '" + fileEntry.FullName + "') already exist, indexes must be unique");
            }
            // Check if file exist at specified location
            if (File.Exists(fileEntry.FullName) == false)
            {
                throw new FileNotFoundException("The specified file ('" + fileEntry.Name + "') was not found", fileEntry.FullName);
            }
            Archiver ptr = this;

            fileEntry.Root = this;
            _files.Add(fileEntry);
            // Cabinet needs to be recreated
            while (ptr != null)
            {
                ptr._isDirty = true;
                ptr          = ptr._root;
            }
        }
Example #4
0
 /// <summary>
 /// Add a sub archive to the archive
 /// </summary>
 public void AddArchive(Archiver arc)
 {
     _subArchives.Add(arc);
     arc._root = this;
 }