IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

VSTO utilisation d'une classe [Débutant]


Sujet :

C#

  1. #1
    Membre régulier
    Profil pro
    Pôle Etude et Automatisation
    Inscrit en
    Avril 2007
    Messages
    166
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Pôle Etude et Automatisation
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2007
    Messages : 166
    Points : 100
    Points
    100
    Par défaut VSTO utilisation d'une classe
    Bonjour,

    Je souhaite décompresser des fichiers tar.gz à partir d'Excel en utilisant VSTO.

    J'ai d’abord créé un userform pour récupérer le nom du fichier tar.gz et le chemin du dossier dans lequel les fichiers doivent se décompresser:

    Nom : img01.png
Affichages : 181
Taille : 12,5 Ko

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
     
    namespace ExcelWorkbook1
    {
        public partial class frmBdTarGgz : Form
        {
            public frmBdTarGgz()
            {
                InitializeComponent();
            }
     
            private void BtFilename_Click(object sender, EventArgs e)
            {
                var fileContent = string.Empty;
                var filePath = string.Empty;
     
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    openFileDialog.InitialDirectory = "c:\\";
                    openFileDialog.Filter = "tar.gz files (*.tar.gz)|*tar.gz";
                    openFileDialog.FilterIndex = 2;
                    openFileDialog.RestoreDirectory = true;
     
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        //Get the path of specified file
                        filePath = openFileDialog.FileName;
     
                        //Read the contents of the file into a stream
                        var fileStream = openFileDialog.OpenFile();
     
                        using (StreamReader reader = new StreamReader(fileStream))
                        {
                            fileContent = reader.ReadToEnd();
                        }
                    }
                }
     
                textBox1.Text = filePath;
     
            }
     
            private void BtOutputDir_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.ShowDialog();
     
                textBox2.Text = folderBrowserDialog.SelectedPath;
            }
     
            private void BtCancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
     
            private void BtExtract_Click(object sender, EventArgs e)
            {
     
            }
        }
    }
    Je souhaite utiliser la class ci-dessous pour décompresser:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.IO.Compression;
     
    /*
        This software is available under 2 licenses -- choose whichever you prefer.
        ------------------------------------------------------------------------------
        ALTERNATIVE A - MIT License
        Copyright (c) 2017 Sean Barrett
        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
        of the Software, and to permit persons to whom the Software is furnished to do
        so, subject to the following conditions:
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        ------------------------------------------------------------------------------
        ALTERNATIVE B - Public Domain (www.unlicense.org)
        This is free and unencumbered software released into the public domain.
        Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
        software, either in source code form or as a compiled binary, for any purpose,
        commercial or non-commercial, and by any means.
        In jurisdictions that recognize copyright laws, the author or authors of this
        software dedicate any and all copyright interest in the software to the public
        domain. We make this dedication for the benefit of the public at large and to
        the detriment of our heirs and successors. We intend this dedication to be an
        overt act of relinquishment in perpetuity of all present and future rights to
        this software under copyright law.
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
        ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
        WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     */
     
     
    namespace ExcelWorkbook1
    {
        internal class TarGz
        {
    		/// <summary>
    		/// Extracts a <i>.tar.gz</i> archive to the specified directory.
    		/// </summary>
    		/// <param name="filename">The <i>.tar.gz</i> to decompress and extract.</param>
    		/// <param name="outputDir">Output directory to write the files.</param>
    		public static void ExtractTarGz(string filename, string outputDir)
    		{
    			using (var stream = File.OpenRead(filename))
    				ExtractTarGz(stream, outputDir);
    		}
     
    		/// <summary>
    		/// Extracts a <i>.tar.gz</i> archive stream to the specified directory.
    		/// </summary>
    		/// <param name="stream">The <i>.tar.gz</i> to decompress and extract.</param>
    		/// <param name="outputDir">Output directory to write the files.</param>
    		public static void ExtractTarGz(Stream stream, string outputDir)
    		{
    			// A GZipStream is not seekable, so copy it first to a MemoryStream
    			using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
    			{
    				const int chunk = 4096;
    				using (var memStr = new MemoryStream())
    				{
    					int read;
    					var buffer = new byte[chunk];
    					do
    					{
    						read = gzip.Read(buffer, 0, chunk);
    						memStr.Write(buffer, 0, read);
    					} while (read == chunk);
     
    					memStr.Seek(0, SeekOrigin.Begin);
    					ExtractTar(memStr, outputDir);
    				}
    			}
    		}
     
    		/// <summary>
    		/// Extractes a <c>tar</c> archive to the specified directory.
    		/// </summary>
    		/// <param name="filename">The <i>.tar</i> to extract.</param>
    		/// <param name="outputDir">Output directory to write the files.</param>
    		public static void ExtractTar(string filename, string outputDir)
    		{
    			using (var stream = File.OpenRead(filename))
    				ExtractTar(stream, outputDir);
    		}
     
    		/// <summary>
    		/// Extractes a <c>tar</c> archive to the specified directory.
    		/// </summary>
    		/// <param name="stream">The <i>.tar</i> to extract.</param>
    		/// <param name="outputDir">Output directory to write the files.</param>
    		public static void ExtractTar(Stream stream, string outputDir)
    		{
    			var buffer = new byte[100];
    			while (true)
    			{
    				stream.Read(buffer, 0, 100);
    				var name = Encoding.ASCII.GetString(buffer).Trim('\0');
    				if (String.IsNullOrWhiteSpace(name))
    					break;
    				stream.Seek(24, SeekOrigin.Current);
    				stream.Read(buffer, 0, 12);
    				var size = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, 12).Trim('\0').Trim(), 8);
     
    				stream.Seek(376L, SeekOrigin.Current);
     
    				var output = Path.Combine(outputDir, name);
    				if (!Directory.Exists(Path.GetDirectoryName(output)))
    					Directory.CreateDirectory(Path.GetDirectoryName(output));
    				if (!name.Equals("./", StringComparison.InvariantCulture))
    				{
    					using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.Write))
    					{
    						var buf = new byte[size];
    						stream.Read(buf, 0, buf.Length);
    						str.Write(buf, 0, buf.Length);
    					}
    				}
     
    				var pos = stream.Position;
     
    				var offset = 512 - (pos % 512);
    				if (offset == 512)
    					offset = 0;
     
    				stream.Seek(offset, SeekOrigin.Current);
    			}
    		}
     
     
    	}
    }
    Je ne sais pas adapter le code pour que cela fonctionne.

    Comment appeler la class à partir de:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
            private void BtExtract_Click(object sender, EventArgs e)
            {
     
            }
    et adapter le code pour faire passer les paramétres dans la class.

    Merci pour votre aide

    Eric

  2. #2
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 674
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 674
    Points : 5 259
    Points
    5 259
    Par défaut
    https://docs.microsoft.com/fr-fr/dot...ject-oriented/
    https://docs.microsoft.com/fr-fr/dot...-class-members

    PS :
    Je te suggère fortement de revoir les fondamentaux avant de t'attaquer à VSTO.
    Stocker des infos dans des variables et appeler une méthode (même statique comme ici), c'est la base.

  3. #3
    Membre régulier
    Profil pro
    Pôle Etude et Automatisation
    Inscrit en
    Avril 2007
    Messages
    166
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Pôle Etude et Automatisation
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2007
    Messages : 166
    Points : 100
    Points
    100
    Par défaut
    Merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
            private void BtExtract_Click(object sender, EventArgs e)
            {
                TarGz.ExtractTarGz(textBox1.Text, textBox2.Text);
            }
    Eric

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [POO] Utilisation d'une classe dans une classe !
    Par Okinou dans le forum Langage
    Réponses: 3
    Dernier message: 16/02/2006, 14h34
  2. Réponses: 4
    Dernier message: 08/11/2005, 15h10
  3. ou mettre une structure utilisée par une classe
    Par grand's dans le forum C++
    Réponses: 10
    Dernier message: 28/07/2005, 18h53
  4. Réponses: 8
    Dernier message: 22/12/2004, 22h57
  5. Réponses: 2
    Dernier message: 18/05/2004, 14h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo