public override bool Write(string destFolder)
        {
            string fileName       = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName);
            string fullPathToFile = Path.Combine(destFolder, fileName);
            string root           = Path.GetDirectoryName(fullPathToFile);

            if (Directory.CreateDirectory(root) != null)
            {
                string targetFile           = InternalWriteArchiveEntry.GetTargetFileToLink(_internalEntry.Data);
                string fullPathToTargetFile = Path.Combine(destFolder, targetFile);
                if (WindowsNativeLibrary.CreateSymbolicLink(fullPathToFile, fullPathToTargetFile, SYMBOLIC_LINK_FLAG.File))
                {
                    if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0)
                    {
                        SetSymLinkLastWriteTime(fullPathToFile, _internalEntry.mTime);
                    }
                    return(true);
                }
                else
                {
                    throw new Exception(string.Format("Symbolic link for file {0} no created. Win32Error: {1}", fullPathToFile, Marshal.GetLastWin32Error()));
                }
            }
            return(false);
        }
        public override bool Write(string destFolder)
        {
            string fileName       = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName);
            string fullPathToFile = Path.Combine(destFolder, fileName);
            string root           = Path.GetDirectoryName(fullPathToFile);

            if (Directory.CreateDirectory(root) != null)
            {
                using (FileStream fs = new FileStream(fullPathToFile, FileMode.Create))
                {
                    if (_internalEntry.Data != null)
                    {
                        var data = _internalEntry.Data.Where(g => g != '\0').ToArray();
                        fs.Write(data, 0, data.Length);
                    }
                }

                if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0)
                {
                    File.SetLastWriteTimeUtc(fullPathToFile, _internalEntry.mTime);
                }

                return(true);
            }
            return(false);
        }
        string IArchiveEntryWriter.PostExtractEntryToDisk(string destFolder, List <IReadableCPIOArchiveEntry> entries)
        {
            if (_internalEntry.nLink > 0 && !_internalEntry.IsExtractToDisk)
            {
                if (IsPostExtractEntry())
                {
                    switch (_internalEntry.ArchiveType)
                    {
                    case ArchiveEntryType.FILE:
                    {
                        var hardLinkFiles = entries.Where(a => a.INode == _readableArchiveEntry.INode);
                        ExtractHardlinkFiles(destFolder, hardLinkFiles.ToList());
                    }
                    break;

                    case ArchiveEntryType.SYMBOLIC_LINK:
                    {
                        _readableArchiveEntry.Writer.WriteEntryToDisk(destFolder);
                    }
                    break;

                    default:
                        throw new Exception("Not expected type of entry");
                    }
                }
                else
                {
                    throw new Exception("The entry must be saved to disk already!");
                }
            }
            return(InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName));
        }
        /// <summary>
        /// extract hardlink files
        /// </summary>
        /// <param name="destFolder"></param>
        /// <param name="archiveEntries"></param>
        /// <returns></returns>
        private void ExtractHardlinkFiles(string destFolder, List <IReadableCPIOArchiveEntry> archiveEntries)
        {
            var originalEntry = archiveEntries.FirstOrDefault(a => a.DataSize > 0);

            if (originalEntry == null)
            {
                throw new Exception("Not found file with data for creating hardlink file");
            }
            originalEntry.Writer.WriteEntryToDisk(destFolder);

            archiveEntries.Remove(originalEntry);
            foreach (var entry in archiveEntries)
            {
                (entry.Writer as HardLinkEntryWriter).OriginalFilePath = InternalWriteArchiveEntry.GetFileName(originalEntry.FileName);
                entry.Writer.WriteEntryToDisk(destFolder);
            }
        }
 string IArchiveEntryWriter.ExtractEntryToDisk(string destFolder)
 {
     if (_internalEntry.nLink > 0 && !_internalEntry.IsExtractToDisk)
     {
         if (IsPostExtractEntry())
         {
             return(InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName));
         }
         else
         {
             return(_readableArchiveEntry.Writer.WriteEntryToDisk(destFolder));
         }
     }
     else
     {
         return(null);
     }
 }
示例#6
0
        public override bool Write(string destFolder)
        {
            string fileName       = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName);
            string fullPathToFile = Path.Combine(destFolder, fileName);
            string root           = Path.GetDirectoryName(fullPathToFile);

            if (Directory.CreateDirectory(root) != null)
            {
                if (OriginalFilePath == null)
                {
                    // write as simple file
                    using (FileStream fs = new FileStream(fullPathToFile, FileMode.Create))
                    {
                        if (_internalEntry.Data != null)
                        {
                            var data = _internalEntry.Data.Where(g => g != '\0').ToArray();
                            fs.Write(data, 0, data.Length);
                        }
                    }
                    return(true);
                }
                else
                {
                    string fullPathToTargetFile = Path.Combine(destFolder, OriginalFilePath);
                    if (WindowsNativeLibrary.CreateHardLink(fullPathToFile, fullPathToTargetFile, IntPtr.Zero))
                    {
                        if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0)
                        {
                            File.SetLastWriteTimeUtc(fullPathToFile, _internalEntry.mTime);
                        }
                        return(true);
                    }
                    else
                    {
                        throw new Exception(string.Format("Hardlink for file {0} no created. Win32Error: {1}", fullPathToFile, Marshal.GetLastWin32Error()));
                    }
                }
            }
            return(false);
        }
        public override bool Write(string destFolder)
        {
            string dir           = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName);
            var    d             = Directory.GetParent(dir);
            string fullPathToDir = Path.Combine(destFolder, dir);

            if (Directory.CreateDirectory(fullPathToDir) != null)
            {
                if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0)
                {
                    string currentDir = fullPathToDir;
                    string _dir       = dir;
                    do
                    {
                        Directory.SetLastWriteTimeUtc(currentDir, _internalEntry.mTime);
                        _dir       = Path.GetDirectoryName(_dir);
                        currentDir = Path.GetDirectoryName(currentDir);
                    }while (!string.IsNullOrEmpty(_dir) && _dir != ".");
                }
                return(true);
            }
            return(false);
        }
 string IArchiveEntryWriter.WriteEntryToDisk(string destFolder)
 {
     _internalEntry.IsExtractToDisk = Write(destFolder);
     return(_internalEntry.IsExtractToDisk ? InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName) : null);
 }