[T] Split de fichiers

Partagez vos scripts, et vos applications AutoIt.
Règles du forum
.
Répondre
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

[T] Split de fichiers

#1

Message par Tlem »

Message transféré de l'ancien forum.
Auteur : sylvanie
Date : 12/03/2007


Citation :

Bonjour, ayant eu un jour à coder ce spliter pour une occasion particulière, autant en faire profiter ;)

Le pb avec la plupart des spliter et qu'ils mettent des entêtes proprio, de tels sorte qu'il faut le même soft pour recoller
C'est pour ça que j'ai fait cette version qui découpe "réellement" par octets. Ainsi on peut recoller les morceaux sous différents OS par copy /B, Cat ...

Code : Tout sélectionner

; script de split de fichier
AutoItSetOption("OnExitFunc", "My_Exit")
Global $taille_decoupe = 734003200
Global $to_change = 1
Global $disk = 0, $cd = 1, $pers = 3
Global $old_check = $cd
Global $nom_fic = ""
Global $nom_fic2 = ""
Global $nb_decoup
Global $title_win = "Split file"
Global $last_onglet = 0
Global $coeff = 0.25
Global $Total_size
Global $pid = 0
#region --- GuiBuilder code Start ---
; Script generated by AutoBuilder 0.6 Prototype
#include <GuiConstants.au3>
$hwd = GUICreate($title_win, 400, 350, -1, -1)
$onglet_principal = GUICtrlCreateTab(10, 10, 380, 330)
$tab0 = GUICtrlCreateTabItem("decouper")
$browse = GUICtrlCreateButton("...", 50, 50, 30, 25)
$label_nom_fic = GUICtrlCreateInput("", 100, 50, 270, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$label_poids = GUICtrlCreateLabel("", 50, 80, 370, 25)
$taille_disquette = GUICtrlCreateRadio("Disquette 1,4 Mo", 100, 100, 100, 25)
$taille_CD = GUICtrlCreateRadio("CD 700 Mo", 100, 125, 100, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
$taille_perso = GUICtrlCreateRadio("Taille perso (en octet):", 100, 200, 200, 25)
$perso = GUICtrlCreateInput("", 100, 225, 100, 25, $ES_NUMBER)
GUICtrlSetState($perso, $GUI_DISABLE)
GUICtrlSetLimit($perso, 9)
$bt_split = GUICtrlCreateButton("Split", 300, 300, 50, 25)
$tab1 = GUICtrlCreateTabItem("rassembler")

$browse2 = GUICtrlCreateButton("...", 50, 50, 30, 25)
$label_nom_fic2 = GUICtrlCreateInput("", 100, 50, 270, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$bt_colle = GUICtrlCreateButton("Recoller", 300, 300, 50, 25)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $onglet_principal
            If GUICtrlRead($onglet_principal) <> $last_onglet Then
                $last_onglet = GUICtrlRead($onglet_principal)
            EndIf
        Case Else
            ;;;
    EndSelect
    If ($last_onglet == 0) Then
        If $msg = $bt_split Then
            If ($nom_fic <> "") And (lecture_taille($taille_decoupe) <> 0) Then
                GUICtrlSetState($onglet_principal, $GUI_DISABLE)
                Split_file($nom_fic, $taille_decoupe)
                GUICtrlSetState($onglet_principal, $GUI_ENABLE)
                WinSetTitle($title_win, "", "Split file")
                $title_win = "Split file"
            Else
                MsgBox(16, "Erreur d argument", "Sélectionnez un fichier et la taille du découpage")
            EndIf
        EndIf

        If $msg = $browse Then
            Select_file($nom_fic)
            GUICtrlSetData($label_nom_fic, $nom_fic)
            If $nom_fic = "" Then
                GUICtrlSetData($label_poids, "")
            Else
                maj_size($nom_fic, $taille_decoupe)
            EndIf
        EndIf

        If $taille_disquette And BitAND(GUICtrlRead($taille_disquette), $GUI_CHECKED) = $GUI_CHECKED Then
            If $old_check <> $disk Then
                GUICtrlSetState($perso, $GUI_DISABLE)
                AdlibDisable()
                maj_size($nom_fic, $taille_decoupe)
                $old_check = $disk
            EndIf
        EndIf

        If $taille_CD And BitAND(GUICtrlRead($taille_CD), $GUI_CHECKED) = $GUI_CHECKED Then
            If $old_check <> $cd Then
                GUICtrlSetState($perso, $GUI_DISABLE)
                AdlibDisable()
                maj_size($nom_fic, $taille_decoupe)
                $old_check = $cd
            EndIf
        EndIf

        If $taille_perso And BitAND(GUICtrlRead($taille_perso), $GUI_CHECKED) = $GUI_CHECKED Then
            If $old_check <> $pers Then
                GUICtrlSetState($perso, $GUI_ENABLE)
                maj_size($nom_fic, $taille_decoupe)
                AdlibEnable("maj_size_perso", 500)
                $old_check = $pers
            EndIf
        EndIf
    Else
        If $msg = $browse2 Then
            Select_file2($nom_fic2)
            GUICtrlSetData($label_nom_fic2, $nom_fic2)
        EndIf

        If $msg = $bt_colle Then
            If $nom_fic2 == "" Then
                MsgBox(16, "Erreur", "Sélectionnez un fichier .decoupe à recoller")
            Else
                GUICtrlSetState($onglet_principal, $GUI_DISABLE)
                colle($nom_fic2)
                GUICtrlSetState($onglet_principal, $GUI_ENABLE)
                WinSetTitle($title_win, "", "Split file")
                $title_win = "Split file"
            EndIf
        EndIf
    EndIf

WEnd
Exit
#endregion --- GuiBuilder code Start ---

Func maj_size_perso()
    lecture_taille($taille_decoupe)
    If ($nom_fic == "") Or (lecture_taille($taille_decoupe) == 0) Then
        GUICtrlSetData($label_poids, "")
    Else
        $nb_decoup = Int(FileGetSize($nom_fic) / $taille_decoupe)
        If Mod(FileGetSize($nom_fic), $taille_decoupe) <> 0 Then $nb_decoup = $nb_decoup + 1
        GUICtrlSetData($label_poids, StringFormat("%.2f", FileGetSize($nom_fic) / 1048576) & " Mo, Nbre de découpage : " & $nb_decoup)
    EndIf
    Return
EndFunc   ;==>maj_size_perso
Func maj_size($nom_fic, $taille_decoupe)
    lecture_taille($taille_decoupe)
    If ($nom_fic == "") Or ($taille_decoupe == 0) Then
        GUICtrlSetData($label_poids, "")
    Else
        $nb_decoup = Int(FileGetSize($nom_fic) / $taille_decoupe)
        ;MsgBox (0,"debug",FileGetSize($nom_fic)&" "&$taille_decoupe&" "&$nb_decoup)
        If Mod(FileGetSize($nom_fic), $taille_decoupe) <> 0 Then $nb_decoup = $nb_decoup + 1
        GUICtrlSetData($label_poids, StringFormat("%.2f", FileGetSize($nom_fic) / 1048576) & " Mo, Nbre de découpage : " & $nb_decoup)
    EndIf
    Return
EndFunc   ;==>maj_size

Func lecture_taille(ByRef $taille_decoupe)
    If BitAND(GUICtrlRead($taille_disquette), $GUI_CHECKED) = $GUI_CHECKED Then $taille_decoupe = 1509949
    If BitAND(GUICtrlRead($taille_CD), $GUI_CHECKED) = $GUI_CHECKED Then $taille_decoupe = 734003200
    If BitAND(GUICtrlRead($taille_perso), $GUI_CHECKED) = $GUI_CHECKED Then
        $taille_decoupe = GUICtrlRead($perso)
        If $taille_decoupe = "" Then $taille_decoupe = 0
    EndIf
    Return $taille_decoupe
EndFunc   ;==>lecture_taille
Func Select_file(ByRef $nom_fic)
    $nom_fic = FileOpenDialog("Fichier à spliter", @MyDocumentsDir, "All (*.*)", 1)
    If @error == 1 Then $nom_fic = ""
    Return
EndFunc   ;==>Select_file
Func Select_file2(ByRef $nom_fic2)
    $nom_fic2 = FileOpenDialog("Fichier à recoller", @MyDocumentsDir, "Decoupe (*.decoupe)", 1)
    If @error == 1 Then $nom_fic2 = ""
    Return
EndFunc   ;==>Select_file2
Func Split_file($nomfic, $taille)
    Local $taille_fichier = 0
    Local $buffer_interm = ""
    Local $hd_fic, $hd_fic2
    Local $cpt = 1
    Local $nomfic2
    Local $ctrl
    Local $pas
    Local $min_taille
    Local $Ram = 1048576
    Local $indice
    Local $taille_traitee

    If Not FileExists($nomfic) Then
        MsgBox(16, "Erreur", "Le fichier " & $nomfic & " n'existe pas")
        Return 0
    EndIf

    $taille_fichier = FileGetSize($nomfic)
    If @error == 1 Then
        MsgBox(16, "Erreur", "Erreur de détermination de la taille du fichier")
        Return 0
    EndIf
    ; vérification si la ram dispo est > à la longueur d allocation

    If $taille < $Ram Then
        $min_taille = ($taille)
        $pas = 1
    Else
        $min_taille = $Ram
        $pas = Int($taille / $min_taille)
        If (Mod($taille, $min_taille) <> 0) Then $pas = $pas + 1
    EndIf

    $hd_fic = FileOpen($nomfic, 0)
    If $hd_fic == -1 Then
        MsgBox(16, "Erreur", "Erreur d'ouverture du fichier")
        Return 0
    EndIf

    While ($taille_fichier > 0)
        WinSetTitle($title_win, "", "Split file " & Int(($cpt - 1) / $nb_decoup * 100) & " %")
        $title_win = "Split file " & Int(($cpt - 1) / $nb_decoup * 100) & " %"
        $nomfic2 = $nomfic & "." & StringFormat("%03d", $cpt)
        $hd_fic2 = FileOpen($nomfic2, 2)
        If $hd_fic2 == -1 Then
            MsgBox(16, "Erreur", "Erreur de creation de fichier intermediaire")
            FileClose($hd_fic)
            Return 0
        EndIf
        $taille_traitee = 0
        For $indice = 1 To $pas - 1
            WinSetTitle($title_win, "", "Split file " & Int(($cpt - 1 + $indice / $pas) / $nb_decoup * 100) & " %")
            $title_win = "Split file " & Int(($cpt - 1 + $indice / $pas) / $nb_decoup * 100) & " %"
            $buffer_interm = FileRead($hd_fic, $min_taille)
            If @error == 1 Then
                MsgBox(16, "Erreur", "Erreur de lecture du fichier")
                FileClose($hd_fic)
                Return 0
            EndIf
            $ctrl = FileWrite($hd_fic2, $buffer_interm)
            $buffer_interm = ""
            If $ctrl == 0 Then
                MsgBox(16, "Erreur", "Erreur d' ecriture dans un fichier intermediaire")
                FileClose($hd_fic)
                FileClose($hd_fic2)
                Return 0
            EndIf
            $taille_traitee = $taille_traitee + $min_taille
        Next
        $taille_traitee = $taille - $taille_traitee
        If $taille_traitee > 0 Then
            WinSetTitle($title_win, "", "Split file " & Int($cpt / $nb_decoup * 100) & " %")
            $title_win = "Split file " & Int($cpt / $nb_decoup * 100) & " %"
            $buffer_interm = FileRead($hd_fic, $taille_traitee)
            If @error == 1 Then
                MsgBox(16, "Erreur", "Erreur de lecture du fichier")
                FileClose($hd_fic)
                Return 0
            EndIf
            $ctrl = FileWrite($hd_fic2, $buffer_interm)
            $buffer_interm = ""
            If $ctrl == 0 Then
                MsgBox(16, "Erreur", "Erreur d' ecriture dans un fichier intermediaire")
                FileClose($hd_fic)
                FileClose($hd_fic2)
                Return 0
            EndIf
        EndIf
        FileClose($hd_fic2)
        $cpt = $cpt + 1
        $taille_fichier = $taille_fichier - $taille
    WEnd
    $nomfic2 = $nomfic & ".decoupe"
    $hd_fic2 = FileOpen($nomfic2, 2)
    If $hd_fic2 == -1 Then
        MsgBox(16, "Erreur", "Erreur de creation de fichier index")
        FileClose($hd_fic)
        Return 0
    EndIf
    FileWriteLine($hd_fic2, $nb_decoup)
    FileClose($hd_fic2)
    FileClose($hd_fic)
    MsgBox(64, "Fichier découpé", "Le fichier a été correctement découpé")
    Return 1

EndFunc   ;==>Split_file

Func colle($nom_fic2)
    Local $max_split
    Local $nom_interm = StringRegExpReplace($nom_fic2, "\x2e[\w\d\s]+\z", "")
    Local $nom_interm2
    Local $indice
    Local $commande = 'copy /Y'
    Local $taille_interm = 0

    If Not FileExists($nom_fic2) Then
        MsgBox(16, "Erreur", $nom_fic2 & " n'existe pas / plus")
        Return 0
    EndIf
    $max_split = FileReadLine($nom_fic2)
    If Not IsNumber(Number($max_split)) Then
        MsgBox(16, "Erreur", "Le fichier ne contient pas le nombre max de fichiers à recoller")
        Return 0
    EndIf
    $Total_size = 0
    For $indice = 1 To $max_split
        $nom_interm2 = $nom_interm & "." & StringFormat("%03d", $indice)
        If Not FileExists($nom_interm2) Then
            MsgBox(16, "Erreur", "Un fichier intermédiaire est manquant : " & $nom_interm2)
            Return 0
        EndIf
        $Total_size = $Total_size + FileGetSize($nom_interm2)
        $commande = $commande & ' /B "' & $nom_interm2 & '"'
        If $indice < $max_split Then $commande = $commande & ' +'
    Next
    $commande = $commande & ' /B "' & $nom_interm & '"'
    WinSetTitle($title_win, "", "Split file rassemblement en cours 0%")
    $title_win = "Split file rassemblement en cours 0%"
    $pid = Run(@ComSpec & " /c " & $commande, @WorkingDir, @SW_HIDE)
    While ProcessExists($pid) <> 0
        Sleep(1000)
        update_title($taille_interm, $nom_interm)
    WEnd
    $pid = 0
    WinSetTitle($title_win, "", "Split file")
    $title_win = "Split file"
    MsgBox(64, "Recollage", "Rassemblement du fichier OK")
EndFunc   ;==>colle
Func update_title(ByRef $taille_interm, $nom_interm)
    Local $current_size
    $current_size = FileGetSize($nom_interm)
    If @error Then
        MsgBox(16, "Erreur", "Problème d'update de la taille")
        My_Exit()
    EndIf
    WinSetTitle($title_win, "", "Split file rassemblement en cours " & Int($current_size / $Total_size * 100) & "%")
    $title_win = "Split file rassemblement en cours " & Int($current_size / $Total_size * 100) & "%"
    $taille_interm = $current_size
EndFunc   ;==>update_title
Func My_Exit()
    If $pid <> 0 Then ProcessClose($pid)
    Exit
EndFunc   ;==>My_Exit
Répondre