Пример #1
0
        /// <summary>
        /// Copy the contents of a stored file into a physical file
        /// </summary>
        /// <param name="_zfe">Entry information of file to extract</param>
        /// <param name="_filename">Name of file to store uncompressed data</param>
        /// <returns>True if success, false if not.</returns>
        /// <remarks>Unique compression methods are Store and Deflate</remarks>
        public bool ExtractFile(ZipFileEntry _zfe, string _filename)
        {
            // Make sure the parent directory exist
            string path = LongDirectory.GetDirectoryName(_filename);

            if (!LongDirectory.Exists(path))
            {
                LongDirectory.CreateDirectory(path);
            }
            // Check it is directory. If so, do nothing
            if (LongDirectory.Exists(_filename))
            {
                return(true);
            }

            Stream output = new FileStream(_filename, FileMode.Create, FileAccess.Write);
            bool   result = ExtractFile(_zfe, output);

            if (result)
            {
                output.Close();
            }

            LongFile.SetCreationTime(_filename, _zfe.ModifyTime);
            LongFile.SetLastWriteTime(_filename, _zfe.ModifyTime);

            return(result);
        }
Пример #2
0
        private void ProcessFile(SyncFile file)
        {
            string fn = _conn.Path + file.F;

            //_log.Info("create dir : " + LongDirectory.GetDirectoryName(fn));
            LongDirectory.CreateDirectory(LongDirectory.GetDirectoryName(fn));

            if (LongFile.Exists(fn))
            {
                // kludge : for seconds resolution
                var fi = new LongFileInfo(fn);
                var d1 = fi.LastWriteTime.Ticks / _tickfilter;
                var d2 = file.D.Ticks / _tickfilter;
                if (d1 >= d2) // skip if already copied or newer
                {
                    return;
                }
            }
            _connInfo.LastFileNameDownloaded = fn;
            fn += ".!torpedosync";
            // FEATURE : resume semi downloaded file

            // remove old semi downloaded file
            LongFile.Delete(fn);

            if (TorpedoSync.Global.isWindows == false)
            {
                fn = fn.Replace("\\", "/");
            }

            _downloading = true;

            if (downloadfile(file, fn, ClientCommands.Download))
            {
                MoveExistingFileToArchive(file.F);
                // rename downloaded file
                LongFile.Move(fn, _conn.Path + file.F);

                LongFile.SetLastWriteTime(_conn.Path + file.F, file.D);
            }
            _downloading = false;
        }
Пример #3
0
        private bool downloadfile(SyncFile file, string saveto, Func <Connection, SyncFile, long, int, DFile> func)
        {
            long left  = file.S;
            int  retry = 0;
            int  mb    = Global.DownloadBlockSizeMB * Global.MB;

            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            while (left > 0)
            {
                long len   = left;
                long start = file.S - left;
                if (len > mb)
                {
                    len = mb;
                }
                //sw.Reset();
                DFile df = func(_conn, file, start, (int)len);
                //_connInfo.Mbs = (len / sw.ElapsedTicks) / (1000*TimeSpan.TicksPerSecond) ;
                if (df == null)
                {
                    retry++;
                    Thread.Sleep(new Random((int)FastDateTime.Now.Ticks).Next(2000) + 500);
                    if (retry > 10)
                    {
                        _log.Info(" null ");
                        _que.Enqueue(file);
                        //if (LongFile.Exists(saveto))
                        LongFile.Delete(saveto);
                        return(false);
                    }
                }
                else if (df.err == DownloadError.OK)
                {
                    retry = 0;
                    string ifn = saveto;
                    if (TorpedoSync.Global.isWindows == false)
                    {
                        ifn = saveto.Replace("\\", "/");
                    }
                    _log.Info("len = " + len + " , " + saveto.Replace(".!torpedosync", ""));
                    left -= len;
                    LongDirectory.CreateDirectory(LongDirectory.GetDirectoryName(ifn));
                    // save to disk
                    var fs = new FileStream(ifn, FileMode.OpenOrCreate);
                    fs.Seek(0L, SeekOrigin.End);
                    fs.Write(df.data, 0, df.data.Length);
                    fs.Close();
                    if (left == 0)
                    {
                        var fi = new LongFileInfo(saveto);
                        if (fi.Length != file.S)
                        {
                            // FEATURE : file length don't match
                            _log.Info("file length don't match.");
                            return(false);
                        }
                    }
                }
                else if (df.err == DownloadError.OLDER || df.err == DownloadError.LOCKED)
                {
                    _ErrorQue.Enqueue(file);
                    _connInfo.LastFileNameDownloaded = "";
                    _log.Info("Locked/Older : " + saveto);
                    return(false);
                }
                else if (df.err == DownloadError.NOTFOUND)
                {
                    _log.Info("Not Found : " + saveto);
                    _connInfo.LastFileNameDownloaded = "";
                    _ErrorQue.Enqueue(file);
                    LongFile.Delete(saveto);
                    return(false);
                }
            }
            return(true);
        }
Пример #4
0
        private void DownloadAsZipFile()
        {
            SyncFile file = null;

            if (Global.BatchZip)
            {
                int             size    = 0;
                int             mb      = Global.BatchZipFilesUnderMB * Global.MB;
                List <SyncFile> ziplist = new List <SyncFile>();
                while (_que.Count > 10)
                {
                    if (_que.TryPeek(out file))
                    {
                        if (file.S + size < mb &&
                            file.S < Global.SmallFileSize) // small files
                        {
                            _que.TryDequeue(out file);
                            ziplist.Add(file);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (ziplist.Count > 0)
                {
                    try
                    {
                        _downloading = true;
                        var sf = ClientCommands.CreateZip(_conn, ziplist);
                        if (sf != null)
                        {
                            string path = _conn.Path + ".ts" + _S + "Temp" + _S;
                            LongDirectory.CreateDirectory(path);

                            if (downloadfile(sf, path + sf.F, ClientCommands.DownloadZip))
                            {
                                string zfn = path + sf.F;
                                string fn  = sf.F;

                                if (TorpedoSync.Global.isWindows == false)
                                {
                                    zfn = zfn.Replace("\\", "/");
                                }

                                var zf = ZipStorer.Open(zfn, FileAccess.Read);
                                foreach (var z in zf.ReadCentralDir())
                                {
                                    if (TorpedoSync.Global.isWindows)
                                    {
                                        fn = z.FilenameInZip.Replace("/", "\\");
                                    }
                                    else
                                    {
                                        fn = z.FilenameInZip;
                                    }

                                    MoveExistingFileToArchive(fn);
                                    LongDirectory.CreateDirectory(LongDirectory.GetDirectoryName(_conn.Path + fn));
                                    try
                                    {
                                        _log.Info("unzip : " + fn);
                                        //_log.Debug("unzip : " + fn);
                                        using (var fs = new FileStream(_conn.Path + fn, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                                        {
                                            _connInfo.LastFileNameDownloaded = fn;
                                            zf.ExtractFile(z, fs);
                                            fs.Close();
                                        }
                                        var dt = z.ModifyTime;
                                        if (z.Comment != "")
                                        {
                                            //_log.Info($"date {z.Comment}");
                                            if (DateTime.TryParse(z.Comment, out dt) == false)
                                            {
                                                dt = z.ModifyTime;
                                            }
                                        }
                                        var dtt = dt;
                                        if (TorpedoSync.Global.isWindows)
                                        {
                                            dtt = dt.ToUniversalTime();
                                        }
                                        LongFile.SetLastWriteTime(_conn.Path + fn, dtt);
                                    }
                                    catch (Exception ex) { _log.Error(ex); }
                                }
                                zf.Close();
                                ClientCommands.DeleteZip(_conn, sf);
                                _log.Info("Decompress zip done : " + sf.F);
                                LongFile.Delete(zfn);
                            }
                        }
                        _downloading = false;
                    }
                    catch (Exception ex) { _log.Error(ex); }
                }
            }
        }
Пример #5
0
 private static void CreateDir(string filename)
 {
     LongDirectory.CreateDirectory(LongDirectory.GetDirectoryName(filename));
 }