Menu principal

News
C.V.
Contact
Archives

Designed by:
SiteGround web hosting Joomla Templates
Supprimer les tag BOM dans fichier UTF-8 Convertir en PDF Version imprimable Suggérer par mail
Écrit par DOTNET   
22-04-2008
Ce programme vous permettra de supprimer les tags au début des fichiers UTF-8 BOM.
Je fournis également le code source, plus sécure quand on est un peu 'parano' :)
Donc pour touver le binaire, aller dans bin\debut\delete_tag_utf8_bom.exe
 
Fichiers :
 

/*
* Created by SharpDevelop.
* User: hidalgo emmanuel
* Date: 22/04/2008
* Time: 11:01
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

using System;
using System.IO;

namespace delete_tag_utf8_bom
{
    class Program
    {
        public static void Main(string[] args)
        {
            if( args.Length == 0 ){
                Console.WriteLine( "Spécifier le chemin du dossier à scanner !!" );
                return;
            }
            if( !Directory.Exists( args[ 0 ] ) ){
                Console.WriteLine( "Le chemin spécifié n'est pas valide !!" );
                return;
            }

            Console.Write( "Suppression des tags UTF-8 BOM en cours..." );
            DirectoryInfo oDirectoryInfo = new DirectoryInfo( args[ 0 ] );
            ScanUtf8Bom oScanUtf8Bom = new ScanUtf8Bom();
            oScanUtf8Bom.ScanFolder( oDirectoryInfo );
        }
    }

    class ScanUtf8Bom{
        public void ScanFolder( DirectoryInfo oDirectory ){
            foreach( FileInfo oFileInfo in oDirectory.GetFiles() )
                if( this.FileIsBOM( oFileInfo.FullName ) )
                    this.DeleteTagBom( oFileInfo.FullName );
            foreach( DirectoryInfo oDirectoryChild in oDirectory.GetDirectories() )
                if( oDirectoryChild.Name != ".svn" )
                    this.ScanFolder( oDirectoryChild );
        }

        public bool FileIsBOM( string sFile ){
            byte[] oData = new Byte[ 3 ];
            byte[] oBom = new Byte[ 3 ];
            oBom[ 0 ] = 239;// -> i
            oBom[ 1 ] = 187;// -> »
            oBom[ 2 ] = 191;// -> ¿

            FileStream oFileStream = new FileStream( sFile, FileMode.Open );
            oFileStream.Read( oData, 0, 3 );

            for( int i = 0; i < oData.Length; i++ )
                if( oData[ i ] != oBom[ i ] )
                    return false;

            oFileStream.Close();

            return true;
        }

        public void DeleteTagBom( string sFile ){
            Console.WriteLine( "delete tag in : " + sFile );

            FileInfo oFileInfo = new FileInfo( sFile );
            byte[] oData = new byte[ oFileInfo.Length ];

            FileStream oFileStream = new FileStream( sFile, FileMode.Open );
            oFileStream.Read( oData, 0, (int)oFileInfo.Length );
            oFileStream.Close();

            File.Delete( sFile );

            oFileStream = new FileStream( sFile, FileMode.Create );
            oFileStream.Write( oData, 3, oData.Length - 3 );
            oFileStream.Close();
        }
    }
}

Dernière mise à jour : ( 12-06-2008 )