Example #1
0
        public static IEnumerable <T> ParseTable <T>(List <List <object> > table, int projectId,
                                                     Func <List <object>, int, int, T> parseRow)
        {
            var rows = new List <T>();

            for (int i = 0; i < table.Count; i++)
            {
                try
                {
                    rows.Add((T)parseRow(table[i], i, projectId));
                }
                catch (Exception e)
                {
                    CommUtils.Assert(false, "文件解析错误(行:" + (i + 1).ToString() + "):" + e.Message);
                }
            }

            return(rows);
        }
Example #2
0
        public static void CompressFiles(string folder, List <string> fileNames, MemoryStream ms)
        {
            CommUtils.Assert(Directory.Exists(folder), "Compress failed, can not find folder [" + folder + "] .");

            ZipOutputStream zipStream = new ZipOutputStream(ms);

            zipStream.SetLevel(3);

            foreach (var file in fileNames)
            {
                var filePath = Path.Combine(folder, file);
                if (!File.Exists(filePath))
                {
                    continue;
                }

                FileInfo fileInfo = new FileInfo(filePath);

                string   entryName = ZipEntry.CleanName(file);
                ZipEntry newEntry  = new ZipEntry(entryName);
                newEntry.DateTime      = fileInfo.LastWriteTime;
                newEntry.Size          = fileInfo.Length;
                newEntry.IsUnicodeText = true;

                zipStream.PutNextEntry(newEntry);

                byte[] buffer = new byte[4096];
                using (FileStream streamReader = File.OpenRead(filePath))
                {
                    StreamUtils.Copy(streamReader, zipStream, buffer);
                }
                zipStream.CloseEntry();
            }

            zipStream.Flush();
            zipStream.Finish();

            ms.Seek(0, SeekOrigin.Begin);
        }
Example #3
0
        static public void Copy(string srcFilePath, string destFilePath, bool checkIsExist = true)
        {
            if (checkIsExist)
            {
                CommUtils.Assert(File.Exists(srcFilePath), "Can't find file [" + srcFilePath + "]");
            }
            else
            {
                if (!File.Exists(srcFilePath))
                {
                    return;
                }
            }

            try
            {
                File.Copy(srcFilePath, destFilePath, true);
            }
            catch (Exception e)
            {
                CommUtils.Assert(false, "Copy [{0}] from [{1}] failed! Exception: {2}", destFilePath, srcFilePath, e.Message);
            }
        }
Example #4
0
        //解析可空日期
        //日期:2016/07/25、2016-07-25、20160725、2016-7-25、2016/7/2
        //空:"-"、""、"  "
        public static DateTime?Parse(string date)
        {
            if (date == null || date.Trim() == "-" || string.IsNullOrWhiteSpace(date))
            {
                return(null);
            }

            if (date.Contains('-') || date.Contains('/'))
            {
                DateTime dt;
                if (DateTime.TryParse(date, out dt))
                {
                    return(dt);
                }
            }
            else if (IsDigitDate(date))
            {
                return(ParseDigitDate(date));
            }

            CommUtils.Assert(false, "解析日期[{0}]失败", date);
            return(null);
        }
Example #5
0
        static public void CheckExtension(string filePathName)
        {
            var extension = Path.GetExtension(filePathName);

            CommUtils.Assert(!string.IsNullOrEmpty(extension), "文件[{0}]中不包含扩展名(文件类型)", filePathName);
        }