Example #1
0
        /// <summary>
        /// Writes the file to a given directory
        /// </summary>
        public void Extract(string Destination)
        {
            // Check to see if this file is null...
            if (this.Size == 0)
            {
                System.IO.File.Create(Destination);
                return;
            }

            // Before doing anything else, check to see if hte user has
            // enough space on their current drive for the file
            try
            {
                if (new System.IO.DriveInfo(System.IO.Path.GetPathRoot(Destination)).AvailableFreeSpace < Size)
                {
                    throw new Exception("Not enough free space on destination device to extract file \"" + Name + "\" to");
                }
            }
            catch { }

            // Create a new instance of the blocks occupied
            uint[] Blocks = BlocksOccupied;
            // FileAction to be used
            Structs.FileAction Fa = new FileAction();
            Fa.MaxValue = Blocks.Length;
            Fa.FullPath = this.FullPath;

            // The IO to read the file
            Streams.Reader FileReader = Drive.Reader();
            // The IO to write to the destination file
            Streams.Writer FileWriter = new CLKsFATXLib.Streams.Writer(new System.IO.FileStream(Destination, System.IO.FileMode.Create));
            // Loop for each block...
            for (int i = 0; i < Blocks.Length - 1; i++)
            {
                if (Fa.Cancel)
                {
                    Fa.Cancel = false;
                    FileWriter.Close();
                    return;
                }
                // Set the position to the beginning of the block
                FileReader.BaseStream.Position = VariousFunctions.GetBlockOffset(Blocks[i], this);
                for (int j = 1, k=0; j <= 0x100; j++, k++)
                {
                    if (i + k == Blocks.Length - 1)
                    {
                        FileWriter.Write(FileReader.ReadBytes((int)PartitionInfo.ClusterSize * k));
                        i += k;
                        Fa.Progress += k;
                        break;
                    }
                    else if (Blocks[i + k] == Blocks.Length - 2 || Blocks[i + k] + 1 != Blocks[i + j] || j == 10)
                    {
                        FileWriter.Write(FileReader.ReadBytes((int)PartitionInfo.ClusterSize * j));
                        i += k;
                        Fa.Progress += j;
                        break;
                    }
                }
                OnFileAction(ref Fa);
            }
            // For the last cluster, we don't know how long it is... so we use
            // this nifty function I made to do that for us
            FileReader.BaseStream.Position = VariousFunctions.GetBlockOffset(Blocks[Blocks.Length - 1], this);
            // Read/write data
            FileWriter.Write(VariousFunctions.ReadBytes(ref FileReader, VariousFunctions.RemainingData(this)));
            Fa.Progress++;
            OnFileAction(ref Fa);
            FileWriter.Close();
        }
Example #2
0
 void f_FileAction(ref FileAction Progress)
 {
     fa.MaxValue = Progress.MaxValue;
     fa.Progress = Progress.Progress;
     fa.CurrentFilePath = Progress.FullPath;
     if (fa.Cancel)
     {
         fa.Cancel = false;
         Progress.Cancel = true;
     }
     OnFolderAction(ref fa);
 }
Example #3
0
        /// <summary>
        /// Deletes the file from the parent folder, clears the FAT chain, etc.
        /// </summary>
        public void Delete()
        {
            if (this.IsDeleted)
            {
                return;
            }
            // File action (event)
            Structs.FileAction fa = new FileAction();
            fa.FullPath = this.FullPath;
            fa.MaxValue = 2;
            OnFileAction(ref fa);

            // Create the entry functions so we can clear the FAT chain
            EntryFunctions ef = new EntryFunctions(this);
            if (Size != 0)
            {
                // Get the FAT chain
                uint[] FatChain = this.BlocksOccupied;
                // Clear the FAT chain
                ef.ClearFATChain(FatChain);
                // Add to the remaining space on the drive
                if (Drive.Remaining != ~0UL)
                {
                    Drive.Remaining += (ulong)(FatChain.Length * PartitionInfo.ClusterSize);
                }
                // Dispose of the FatChain
                FatChain = null;
            }

            // Change the progress
            fa.Progress = 1;
            OnFileAction(ref fa);

            // Get the new entry data that we're going to write
            EntryData ed = this.EntryData;
            // Mark it as deleted
            ed.NameSize = 0xE5;
            // "Create" a new entry (writing the old one with the size of 0xE5
            ef.CreateNewEntry(ed);

            // Change progress
            fa.Progress = 2;
            OnFileAction(ref fa);

            // Reset the entry data
            this.EntryData = ed;

            // Other event
            EntryEventArgs eea = new EntryEventArgs();
            eea.FullParentPath = Parent.FullPath;
            eea.ModifiedEntry = this;
            eea.ParentFolder = this.Parent;
            eea.Deleting = true;
            if (Parent != null)
            {
                Parent.cachedFiles.Remove(this);
                Parent.cachedDeletedFiles.Add(this);
                try
                {
                    Parent.OnEntryEvent(ref eea);
                }
                catch { }
            }
        }
Example #4
0
 protected virtual void OnFileAction(ref FileAction a)
 {
     if (FileAction != null)
     {
         FileAction(ref a);
     }
 }