Exemple #1
0
 public Unzip()
     : base()
 {
     SourceFile       = @"[TargetPath]\[TargetName]";
     DestinationPath  = "[DestinationPath]";
     OutputFileExists = Sys.IO.FileExistsAction.Throw;
 }
Exemple #2
0
 public UnGZip()
     : base()
 {
     SourceFile       = "[TargetPath]\\[TargetName]";
     OutputFile       = @"[DestinationPath]\[TargetNameWithoutExt]";
     OutputFileExists = Sys.IO.FileExistsAction.Throw;
 }
        public FileTransfer(string sourceFile, string destinationPath, string destinationFilename, STEM.Sys.IO.FileExistsAction fileExistsAction, bool compress, bool onlySaveIfNewer)
        {
            _SourceFile = sourceFile;

            CreationTimeUtc = LastWriteTimeUtc = DeletedTimeUtc = MinValue;

            OnlySaveIfNewer = onlySaveIfNewer;
            Compressed      = compress;

            FileExistsAction = fileExistsAction;

            DestinationPath     = STEM.Sys.IO.Path.AdjustPath(destinationPath);
            DestinationFilename = STEM.Sys.IO.Path.AdjustPath(destinationFilename);

            RefreshContentFromFile();
        }
        public FileTransfer(DateTime creationTimeUtc, DateTime lastWriteTimeUtc, string destinationPath, string destinationFilename, byte[] fileContent, STEM.Sys.IO.FileExistsAction fileExistsAction, bool compress, bool onlySaveIfNewer)
        {
            _SourceFile = null;

            DeletedTimeUtc = MinValue;

            FileExistsAction = fileExistsAction;

            CreationTimeUtc  = creationTimeUtc;
            LastWriteTimeUtc = lastWriteTimeUtc;

            CreationTimeUtc  = DateTime.Parse(CreationTimeUtc.ToString("G", System.Globalization.CultureInfo.CurrentCulture), System.Globalization.CultureInfo.CurrentCulture);
            LastWriteTimeUtc = DateTime.Parse(LastWriteTimeUtc.ToString("G", System.Globalization.CultureInfo.CurrentCulture), System.Globalization.CultureInfo.CurrentCulture);

            if (Compressed)
            {
                byte[] cmp = STEM.Sys.IO.ByteCompression.Compress(fileContent, fileContent.Length);

                if (cmp == null)
                {
                    cmp = new byte[0];
                }

                FileContent = Convert.ToBase64String(cmp);
            }
            else
            {
                FileContent = Convert.ToBase64String(fileContent);
            }

            DestinationPath     = STEM.Sys.IO.Path.AdjustPath(destinationPath);
            DestinationFilename = STEM.Sys.IO.Path.AdjustPath(destinationFilename);

            OnlySaveIfNewer = onlySaveIfNewer;
            Compressed      = compress;
        }
        public string Save(STEM.Sys.IO.FileExistsAction fileExistsAction)
        {
            lock (this)
                try
                {
                    if (String.IsNullOrEmpty(Filepath) || String.IsNullOrEmpty(Filename))
                    {
                        return(null);
                    }

                    string file = STEM.Sys.IO.Path.AdjustPath(System.IO.Path.Combine(Filepath, Filename));

                    if (!System.IO.Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(file)))
                    {
                        System.IO.Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(file));
                    }

                    if (_Content != null)
                    {
                        if (System.IO.File.Exists(file))
                        {
                            switch (fileExistsAction)
                            {
                            case FileExistsAction.Skip:
                                return(null);

                            case FileExistsAction.Throw:
                                throw new Exception("File " + file + " already exists.");

                            case FileExistsAction.MakeUnique:
                                file = STEM.Sys.IO.File.UniqueFilename(file);
                                break;
                            }
                        }

                        while (true)
                        {
                            try
                            {
                                if (!System.IO.Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(file)))
                                {
                                    System.IO.Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(file));
                                }
                            }
                            catch { }

                            try
                            {
                                if (System.IO.Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(file)))
                                {
                                    System.IO.File.WriteAllBytes(file, _Content);
                                    System.IO.File.SetCreationTimeUtc(file, CreationTimeUtc);
                                    System.IO.File.SetLastWriteTimeUtc(file, LastWriteTimeUtc);
                                }
                                else
                                {
                                    return(null);
                                }

                                break;
                            }
                            catch { }
                        }

                        return(file);
                    }
                }
                catch (Exception ex)
                {
                    if (fileExistsAction == FileExistsAction.Throw)
                    {
                        throw new Exception("FileDescription.Save", ex);
                    }
                }

            return(null);
        }
        string Save(string destination, STEM.Sys.IO.FileExistsAction fileExistsAction)
        {
            if (FileContent == null)
            {
                return(null);
            }

            destination = STEM.Sys.IO.Path.AdjustPath(destination);

            try
            {
                if (System.IO.File.Exists(destination))
                {
                    if (fileExistsAction == Sys.IO.FileExistsAction.Skip)
                    {
                        return(null);
                    }

                    if (fileExistsAction == Sys.IO.FileExistsAction.Throw)
                    {
                        throw new System.IO.IOException("Destination file already exists: " + destination);
                    }
                }
            }
            catch { }

            if (!System.IO.Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(destination)))
            {
                System.IO.Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(destination));
            }

            string tmp = System.IO.Path.GetTempFileName();

            try
            {
                if (Compressed)
                {
                    byte[] b = Convert.FromBase64String(FileContent);
                    System.IO.File.WriteAllBytes(tmp, STEM.Sys.IO.ByteCompression.Decompress(b, b.Length));
                }
                else
                {
                    System.IO.File.WriteAllBytes(tmp, Convert.FromBase64String(FileContent));
                }

                System.IO.File.SetCreationTimeUtc(tmp, CreationTimeUtc);
                System.IO.File.SetLastWriteTimeUtc(tmp, LastWriteTimeUtc);

                try
                {
                    if (System.IO.File.Exists(destination))
                    {
                        if (fileExistsAction == Sys.IO.FileExistsAction.Skip)
                        {
                            return(null);
                        }

                        if (fileExistsAction == Sys.IO.FileExistsAction.Throw)
                        {
                            throw new System.IO.IOException("Destination file already exists: " + destination);
                        }

                        if (fileExistsAction == Sys.IO.FileExistsAction.Overwrite)
                        {
                            try
                            {
                                if (onFileModifying != null)
                                {
                                    onFileModifying(this.MessageConnection, Clone());
                                }
                            }
                            catch (Exception ex)
                            {
                                STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileModifying", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                            }

                            int retry = 0;
                            while (true)
                            {
                                try
                                {
                                    System.IO.File.SetAttributes(destination, System.IO.FileAttributes.Normal);
                                    System.IO.File.Copy(tmp, destination, true);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    if (++retry > 3)
                                    {
                                        throw new System.IO.IOException("Destination file could not be overwritten: " + destination, ex);
                                    }

                                    System.Threading.Thread.Sleep(1000);
                                }
                            }

                            try
                            {
                                if (onFileModified != null)
                                {
                                    onFileModified(this.MessageConnection, Clone());
                                }
                            }
                            catch (Exception ex)
                            {
                                STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileModified", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                            }
                        }
                        else if (fileExistsAction == Sys.IO.FileExistsAction.MakeUnique)
                        {
                            string d = destination;
                            while (true)
                            {
                                if (!System.IO.File.Exists(d))
                                {
                                    try
                                    {
                                        DestinationFilename = STEM.Sys.IO.Path.GetFileName(d);

                                        try
                                        {
                                            if (onFileAdding != null)
                                            {
                                                onFileAdding(this.MessageConnection, Clone());
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileAdding", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                                        }

                                        System.IO.File.Copy(tmp, d, false);

                                        try
                                        {
                                            if (onFileAdded != null)
                                            {
                                                onFileAdded(this.MessageConnection, Clone());
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileAdded", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                                        }

                                        return(d);
                                    }
                                    catch { }
                                }

                                d = STEM.Sys.IO.File.UniqueFilename(destination);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            try
                            {
                                if (onFileAdding != null)
                                {
                                    onFileAdding(this.MessageConnection, Clone());
                                }
                            }
                            catch (Exception ex)
                            {
                                STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileAdding", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                            }

                            System.IO.File.Copy(tmp, destination, false);

                            try
                            {
                                if (onFileAdded != null)
                                {
                                    onFileAdded(this.MessageConnection, Clone());
                                }
                            }
                            catch (Exception ex)
                            {
                                STEM.Sys.EventLog.WriteEntry("FileTransfer.onFileAdded", ex.ToString(), Sys.EventLog.EventLogEntryType.Error);
                            }

                            return(destination);
                        }
                        catch (Exception ex)
                        {
                            if (System.IO.File.Exists(destination))
                            {
                                return(Save(destination, fileExistsAction));
                            }

                            throw ex;
                        }
                    }
                }
                catch { }
            }
            finally
            {
                int retry = 0;
                while (System.IO.File.Exists(tmp) && retry++ < 5)
                {
                    try
                    {
                        System.IO.File.Delete(tmp);
                        break;
                    }
                    catch { System.Threading.Thread.Sleep(1000); }
                }
            }

            return(null);
        }