Esempio n. 1
0
 public string GenerateHash(string fileName)
 {
     HachaCRC crc = new HachaCRC (new FileInfo (fileName).Length);
     crc.Reset();
     UtilidadesFicheros.GenerateHash (fileName, crc);
     return crc.Value.ToString ("X").ToUpper();
 }
Esempio n. 2
0
        protected void Partir(string fichero, string s1, string dir, long kb, string version)
        {
            CRC crc = null;
            if (version == "1") {
                crc = new NullHachaCRC ();
            } else {
                crc = new HachaCRC (new FileInfo (fichero).Length);
            }

            CabeceraHacha_v1 cab = CabeceraHacha_v1.NewFromVersion (version);

            cab.Tamano = new FileInfo (fichero).Length;

            // TODO: Conflicto cuando se especifica el numero de fragmentos?
            // cab.TamanoFragmento = kb*1024 - 512;
            cab.TamanoFragmento = kb * 1024;

            cab.NombreOriginal = new FileInfo (fichero).Name;

            if ((s1 == null) || (s1 == string.Empty)) {
                s1 = new FileInfo (fichero).Name;
            }

            string salida1 = dir + Path.DirectorySeparatorChar + s1;

            UtilidadesFicheros.ComprobarSobreescribir(salida1 + ".0");

            //Escribimos la cabecera (para reservar sitio).
            UtilidadesFicheros.Append (salida1 + ".0", cab.ToByteArray());
            OnProgress (0,1);
            long transferidos = 0;
            int fragmento = 0;

            do{
                string s = salida1 + "." + fragmento;
                if (fragmento != 0) {
                    UtilidadesFicheros.ComprobarSobreescribir (s);
                }

                transferidos += UtilidadesFicheros.CopiarIntervalo (
                    fichero, s, transferidos, cab.TamanoFragmento, crc);
                fragmento++;
                OnProgress (transferidos, cab.Tamano);

            } while (transferidos < cab.Tamano);

            // Volvemos a poner la cabecera, con el CRC bien.
            cab.CRC = unchecked ((int)crc.Value);
            UtilidadesFicheros.Sobreescribir (salida1 + ".0", cab.ToByteArray(), 0);
        }
Esempio n. 3
0
        protected void UnirHacha(string fichero, string dirDest)
        {
            int leidos = 0;
            byte[] buffer = new byte[Consts.BUFFER_LENGTH];
            CabeceraHacha_v1 cab = CabeceraHacha_v1.LeerCabecera (fichero);
            int cabSize = cab.Size;

            HachaCRC crcHacha = null;
            Crc32 crc32 = null;
            if (cab.Version.Equals("2") && cab.CRC != 7 && cab.CRC != 0)
            {
                crcHacha = new HachaCRC (cab.Tamano);
                crc32 = new Crc32 ();
            }

            string salida = dirDest + Path.DirectorySeparatorChar + cab.NombreOriginal;

            string b = fichero.Substring (0, fichero.Length - 1);
            OnProgress (0, 1);
            int fragmento = 0;
            long transferidos = 0;
            string fich = b + fragmento;
            Stream outStream = UtilidadesFicheros.CreateWriter (salida);
            while (File.Exists (fich))
            {
                int parcial = 0;
                Stream inStream = File.OpenRead (fich);
                if (fragmento == 0) {
                    if (inStream.Read (buffer, 0, cabSize) != cabSize){
                        throw new IOException ("Premature end of file:" + fich);
                    }
                }

                while ((leidos = inStream.Read (buffer, 0, buffer.Length)) > 0)	{
                    outStream.Write (buffer, 0, leidos);
                    if (crc32 != null){
                        crcHacha.Update (buffer, 0, leidos);
                        crc32.Update (buffer, 0, leidos);
                    }
                    transferidos += leidos;
                    parcial += leidos;
                    OnProgress (transferidos, cab.Tamano);
                }

                if (parcial != cab.TamanoFragmento && transferidos != cab.Tamano) {
                    throw new IOException ("Premature end of file:" + fich);
                }

                fragmento++;
                fich = b + fragmento;
            }
            outStream.Close ();
            if (crc32 != null && cab.CRC != crc32.Value && cab.CRC != crcHacha.Value){
                // TODO Lanzar excepcion
                Console.WriteLine("crc verification failed!");
                //throw new Exception("CRC verification failed");
            }
            OnProgress (cab.Tamano, cab.Tamano);
        }