Beispiel #1
0
        public static string GetMD5Code(string filePath)
        {
            FileStream stream = null;
            try
            {

                stream = File.OpenRead(filePath);
            }
            catch
            {
                try
                {
                    stream.Close();
                    stream.Dispose();
                }
                catch { }
                return Consts.EmptyFileMD5;
            }
            if (stream.Length == 0)
            {
                stream.Close();
                stream.Dispose();
                return Consts.EmptyFileMD5;
            }
            byte[] data = new byte[10240];
            long size = stream.Length;
            int sizetoread;
            MD5Hash md5 = new MD5Hash();
            md5.InitNewHash();

            while (size != 0)
            {
                sizetoread = size > 10240 ? 10240 : (int)size;

                stream.Read(data, 0, sizetoread);
                //data = reader.ReadBytes((int)sizetoread);

                size -= sizetoread;

                if (size == 0)
                    md5.UpdateHash(data, sizetoread, true);
                else
                    md5.UpdateHash(data, sizetoread, false);
            }
            stream.Close();
            stream.Dispose();

            md5.FinalizeHash();
            
            return MD5Hash.FormatHash(md5.GetFinalHash(), 0, 0, 0, 1);//0��ָ0�зָ0�ظ����ٴΣ�1��д���(0Сд)myxbing
        }
Beispiel #2
0
        //public static void TryCreateFileAndIcon(string fullPath, Stream fileStream, int iconID)
        //{
        ////TODO:
        //    if (fullPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
        //    {
        //        string tempPath = Bbs3Globals.TempPath + "\\" + Guid.NewGuid().ToString("N") + ".exe";
        //        CreateFile(tempPath, fileStream);

        //        TryCreateIcon(fullPath, iconID);

        //        File.Move(tempPath, fullPath);
        //    }
        //    else
        //    {
        //        if (!File.Exists(fullPath))
        //            CreateFile(fullPath, fileStream);
        //    }

        //}



        //public static void TryCreateFileAndIcon(string fullPath, Stream fileStream, Guid iconID, bool alwaysCreateIcon)
        //{

        //    string iconPath = Bbs3Globals.DiskFilesPath + "\\icons\\" + iconID + ".gif";
        //    bool fileExists = File.Exists(fullPath);
        //    bool iconExists = File.Exists(iconPath);

        //    if (fileExists && iconExists)
        //    {
        //        fileStream.Close();
        //        fileStream.Dispose();
        //        return;
        //    }

        //    //�������exe�ļ�
        //    if (!fullPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
        //    {
        //        if (alwaysCreateIcon)
        //        {
        //            //������ʱ��exe�ļ�
        //            string tempPath = Globals.GetPath(SystemDirecotry.Temp) + "\\icontemp-" + Guid.NewGuid().ToString("N") + ".exe";
        //            CreateFile(tempPath, fileStream);

        //            //����ʱ��exe�ļ�����icon
        //            TryCreateExeFileIcon(tempPath, iconID);

        //            //�Ѵ�����ͼ����ļ��ƶ�������Ŀ¼
        //            if (!fileExists)
        //                File.Move(tempPath, fullPath);
        //        }
        //        else
        //            CreateFile(fullPath, fileStream);
        //    }
        //    else
        //    {
        //        if (!fileExists)
        //            CreateFile(fullPath, fileStream);
        //    }

        //}
/*
        /// <summary>
        /// ���Դ�EXE�ļ���ȡ������EXE��ͼ��
        /// </summary>
        /// <param name="exeFilepath"></param>
        /// <param name="iconID"></param>
        public static void TryCreateExeFileIcon(string exeFilepath, Guid iconID)
        {
            TryCreateExeFileIcon(exeFilepath, iconID, true, true);
        }
        public static void TryCreateExeFileIcon(string exeFilepath, Guid iconID, bool createSmallIcon, bool createBigIcon)
        {
            string iconPath = null;
            if (createSmallIcon)
                iconPath = Bbs3Globals.DiskFilesPath + "\\icons\\" + iconID + ".gif";
            string largeIconPath = null;
            if (createBigIcon)
                largeIconPath = Bbs3Globals.DiskFilesPath + "\\icons\\big\\" + iconID + ".gif";

            TryCreateExeFileIcon(exeFilepath, iconPath, largeIconPath);
            /*
            //�������exe��׺
            if (!exeFilepath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                return;

            string iconPath = Bbs3Globals.DiskFilesPath + "\\icons\\" + iconID + ".gif";
            string largeIconPath = Bbs3Globals.DiskFilesPath + "\\icons\\big\\" + iconID + ".gif";

            if (File.Exists(iconPath) && File.Exists(largeIconPath))
            {
                return;
            }

            #region ��������ڸ�Сͼ��,����ȡ����
            if (!File.Exists(iconPath))
            {
                Icon icon = zzbird.Common.Disk.IconManager.GetIcon(exeFilepath);
                if (icon != null)
                {
                    using (Bitmap map = icon.ToBitmap())
                    {
                        string directory = Bbs3Globals.DiskFilesPath + "\\icons";
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }
                        map.Save(iconPath);
                    }
                }
            }
            #endregion

            #region ��������ڸô�ͼ��,����ȡ����
            if (!File.Exists(largeIconPath))
            {
                Icon icon = zzbird.Common.Disk.IconManager.GetIcon(exeFilepath, zzbird.Common.Disk.IconManager.GetFileInfoFlags.SHGFI_LARGEICON);
                if (icon != null)
                {
                    using (Bitmap map = icon.ToBitmap())
                    {
                        string directory = Bbs3Globals.DiskFilesPath + "\\icons\\big";
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }
                        map.Save(largeIconPath);
                    }
                }
            }
            #endregion
        }
        */
        /*
        public static void TryCreateExeFileIcon(string exeFilepath, string smallSavePath, string bigSavePath)
        {  //�������exe��׺
            if (!exeFilepath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                return;

            //string iconPath =  savePath +"\\"+iconName+".gif";//Bbs3Globals.DiskFilesPath + "\\icons\\" + iconID + ".gif";
            //string largeIconPath = //Bbs3Globals.DiskFilesPath + "\\icons\\big\\" + iconID + ".gif";

            if (!string.IsNullOrEmpty(smallSavePath))
            {
                smallSavePath = smallSavePath.Replace("/", "\\");
                if (File.Exists(smallSavePath))
                {
                    return;
                }

                #region ��������ڸ�Сͼ��,����ȡ����
                if (!File.Exists(smallSavePath))
                {
                    Icon icon = zzbird.Common.Disk.IconManager.GetIcon(exeFilepath);
                    if (icon != null)
                    {
                        using (Bitmap map = icon.ToBitmap())
                        {
                            string directory = Path.GetDirectoryName(smallSavePath);//smallSavePath.Substring(0,smallSavePath.LastIndexOf("\\")); //Bbs3Globals.DiskFilesPath + "\\icons";
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                            }
                            map.Save(smallSavePath);
                        }
                    }
                }
                #endregion
            }

            if (!string.IsNullOrEmpty(bigSavePath))
            {
                bigSavePath = bigSavePath.Replace("/", "\\");
                if (File.Exists(bigSavePath))
                {
                    return;
                }
                #region ��������ڸô�ͼ��,����ȡ����
                if (!File.Exists(bigSavePath))
                {
                    Icon icon = zzbird.Common.Disk.IconManager.GetIcon(exeFilepath, zzbird.Common.Disk.IconManager.GetFileInfoFlags.SHGFI_LARGEICON);
                    if (icon != null)
                    {
                        using (Bitmap map = icon.ToBitmap())
                        {
                            string directory = Path.GetDirectoryName(bigSavePath);//bigSavePath.Substring(0, smallSavePath.LastIndexOf("\\"));
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                            }
                            map.Save(bigSavePath);
                        }
                    }
                }
                #endregion
            }
        }
*/
        public static string CreateFileAndGetMD5Code(string path, Stream stream, out long length)
        {
            string dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            FileStream fs = new FileStream(path, FileMode.Create);

            //long oldPosition = stream.Position;
            //stream.Position = 0;

            byte[] data = new byte[10240];
            long size = stream.Length;
            length = size;
            int sizetoread;

            MD5Hash md5 = null; ;

            if (length != 0)
            {
                md5 = new MD5Hash();
                md5.InitNewHash();
            }

            while (size != 0)
            {
                sizetoread = size > 10240 ? 10240 : (int)size;

                //try
                //{
                    stream.Read(data, 0, sizetoread);
                //}
                //catch (Exception e)
                //{
                //}
                size -= sizetoread;

                if (size == 0)
                    md5.UpdateHash(data, sizetoread, true);
                else
                    md5.UpdateHash(data, sizetoread, false);

                fs.Write(data, 0, sizetoread);
            }
            fs.Close();
            fs.Dispose();

            if (length == 0)
                return Consts.EmptyFileMD5;
            else
            {
                md5.FinalizeHash();
                return MD5Hash.FormatHash(md5.GetFinalHash(), 0, 0, 0, 1);
            }
        }
Beispiel #3
0
        public static string GetMD5Code(Stream stream)
        {
            if (stream == null || stream.Length == 0)
                return Consts.EmptyFileMD5;

            //return Guid.NewGuid().ToString();
            //next should be rebuilder.
            //BinaryReader reader = new BinaryReader(stream);
            long oldPosition = stream.Position;
            stream.Position = 0;

            byte[] data = new byte[10240];
            long size = stream.Length;
            int sizetoread;
            MD5Hash md5 = new MD5Hash();
            md5.InitNewHash();

            while (size != 0)
            {
                sizetoread = size > 10240 ? 10240 : (int)size;

                stream.Read(data, 0, sizetoread);
                //data = reader.ReadBytes((int)sizetoread);

                size -= sizetoread;

                if (size == 0)
                    md5.UpdateHash(data, sizetoread, true);
                else
                    md5.UpdateHash(data, sizetoread, false);
            }

            md5.FinalizeHash();
            stream.Position = oldPosition;
            //reader.Close();
            //stream.Close();���ܹرգ���������
            return MD5Hash.FormatHash(md5.GetFinalHash(), 0, 0, 0, 1);//0��ָ0�зָ0�ظ����ٴΣ�1��д���(0Сд)myxbing
        }