1. Este site usa cookies. Ao continuar a usar este site está a concordar com o nosso uso de cookies. Saber Mais.

ajuda vb2008

Discussão em 'Programação' iniciada por skullrazor, 24 de Novembro de 2008. (Respostas: 8; Visualizações: 805)

  1. skullrazor

    skullrazor [email protected] Member

    Boas
    Bem eu peguei num bocado de codigo de um programa de compactaçao/descompactaçao de ficheiros em vb, alterei umas coisas, acrescentei outras, mas agora, ao fim de tudo feito, deparei.me com um erro que me da quando eu clico no botao para compactar... nao sei o porquê do erro e agradeço a quem me possa ajudar a resolver o problema, e ja agora se virem mais alguma coisa errada no codigo avisem tambem :) vou postar o codigo do programa e o print do erro... agradeço toda a ajuda que me possam dar...

    O codigo:
    Código:
     
    [SIZE=2][COLOR=#0000ff]Imports [/COLOR][/SIZE][SIZE=2][COLOR=white]System.IO[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Imports [/COLOR][/SIZE][SIZE=2][COLOR=white]System.IO.Compression[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Imports [/COLOR][/SIZE][SIZE=2][COLOR=white]System.Text[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class [/COLOR][/SIZE][SIZE=2][COLOR=white]Form1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamFonte [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] FileStream[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamDestino [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] FileStream[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamCompactado [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GZipStream[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamDescompactado [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GZipStream[/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] compactaArquivo([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] arquivoOrigem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] arquivoDestino [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamFonte [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(arquivoOrigem, FileMode.Open, FileAccess.Read)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamDestino [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(arquivoDestino, FileMode.Create, FileAccess.Write)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamCompactado [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GZipStream(streamDestino, CompressionMode.Compress, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] tamanhoBloco [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 100[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buffer(tamanhoBloco) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bytesLidos [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE]
    [SIZE=2]bytesLidos = streamFonte.Read(buffer, 0, tamanhoBloco)[/SIZE]
    [SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (bytesLidos = 0) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE]
    [SIZE=2]streamCompactado.Write(buffer, 0, bytesLidos)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
    [SIZE=2]MsgBox(ex.Message)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
    [SIZE=2]streamFonte.Close()[/SIZE]
    [SIZE=2]streamCompactado.Close()[/SIZE]
    [SIZE=2]streamDestino.Close()[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] descompactaArquivo([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] arquivoOrigem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] arquivoDestino [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamFonte [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(arquivoOrigem, FileMode.Open, FileAccess.Read)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamDestino [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(arquivoDestino, FileMode.Create, FileAccess.Write)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamDescompactado [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GZipStream(streamFonte, CompressionMode.Decompress, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] tamanhoBloco [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 100[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buffer(tamanhoBloco) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bytesLidos [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE]
    [SIZE=2]bytesLidos = streamDescompactado.Read(buffer, 0, tamanhoBloco)[/SIZE]
    [SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (bytesLidos = 0) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE]
    [SIZE=2]streamDestino.Write(buffer, 0, bytesLidos)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
    [SIZE=2]MsgBox(ex.Message)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
    [SIZE=2]streamFonte.Close()[/SIZE]
    [SIZE=2]streamDescompactado.Close()[/SIZE]
    [SIZE=2]StreamDestino.Close()[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
    [SIZE=2]openfl1.Title = [/SIZE][SIZE=2][COLOR=#a31515]"Open File"[/COLOR][/SIZE]
    [SIZE=2]openfl1.Filter = [/SIZE][SIZE=2][COLOR=#a31515]"All Files (*.*)|*.*"[/COLOR][/SIZE]
    [SIZE=2]savefl1.Title = [/SIZE][SIZE=2][COLOR=#a31515]"Save File"[/COLOR][/SIZE]
    [SIZE=2]savefl1.Filter = [/SIZE][SIZE=2][COLOR=#a31515]".gz Files|*.gz"[/COLOR][/SIZE]
    [SIZE=2]openfl2.Title = [/SIZE][SIZE=2][COLOR=#a31515]"Open File"[/COLOR][/SIZE]
    [SIZE=2]openfl2.Filter = [/SIZE][SIZE=2][COLOR=#a31515]".gz Files|*.gz"[/COLOR][/SIZE]
    [SIZE=2]savefl2.Title = [/SIZE][SIZE=2][COLOR=#a31515]"Save File"[/COLOR][/SIZE]
    [SIZE=2]savefl2.Filter = [/SIZE][SIZE=2][COLOR=#a31515]"All Files (*.*)|*.*"[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] openfile1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] openfile1.Click[/SIZE]
    [SIZE=2]openfl1.ShowDialog()[/SIZE]
    [SIZE=2]TextBox1.Text = openfl1.FileName[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] savefile1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] savefile1.Click[/SIZE]
    [SIZE=2]savefl1.ShowDialog()[/SIZE]
    [SIZE=2]TextBox2.Text = savefl1.FileName[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] bt_compactar_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] bt_compactar.Click[/SIZE]
    [SIZE=2]compactaArquivo(TextBox1.Text, TextBox2.Text)[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] openfile2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] openfile2.Click[/SIZE]
    [SIZE=2]openfl2.ShowDialog()[/SIZE]
    [SIZE=2]TextBox3.Text = openfl2.FileName[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] savefile2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] savefile2.Click[/SIZE]
    [SIZE=2]savefl2.ShowDialog()[/SIZE]
    [SIZE=2]TextBox4.Text = savefl2.FileName[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] bt_descompactar_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] bt_descompactar.Click[/SIZE]
    [SIZE=2]descompactaArquivo(TextBox3.Text, TextBox4.Text)[/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class [/COLOR][/SIZE]
    O print:
    http://img293.imageshack.us/img293/5158/imagem1ar8.jpg

    cumps

    EDIT: tive a exprimentar o programa (mesmo com o erro) e vi que ele me esta a transformar o ficheiro que compacto num ficheiro .gz com um tamanho maior, no ficheiro que eu usei tinha 100mb e passou a 150mb! como é que eu faço para que ao compactar o ficheiro ele me divida o ficheiro .gz em 5 ficheiros .gz com o tamanho do ficheiro dividido pelos 5 ficheiros??

    cumps
     
    Última edição: 24 de Novembro de 2008
  2. skullrazor

    skullrazor [email protected] Member

    alguem ajuda????
     
  3. skullrazor

    skullrazor [email protected] Member

    por favor, alguem ajuda???????
     
  4. fLaSh_CF

    fLaSh_CF Banido

    Por o que vi no print screen..
    Parece me que o objecto não foi inicializado por algum motivo..

    Experimenta com o seguinte codigo:
    Código:
    ........
    Finally
    If streamFontestreamFonte IsNot Nothing Then streamFontestreamFonte.Close()
    If streamDescompactado IsNot Nothing Then streamDescompactado.Close()
    If StreamDestino IsNot Nothing Then StreamDestino.Close()
    EndTry
    ........
    Compr.
     
  5. skullrazor

    skullrazor [email protected] Member

    obrigado pela ajuda desde ja, e resolveu o erro! pelo menos ja nao aparece xD bem mas agora continuo com o problema que postei no edit em cima, podes.me ajudar nessa parte tambem?

    cumps
     
  6. jpaulino

    jpaulino Power Member

  7. skullrazor

    skullrazor [email protected] Member

    realmente desconhecia a simplicidade de criar ficheiros .zip em vb, dai ter usado .gz... bem vou alterar o meu projecto para compactar ficheiroz .zip, mas agora e com estes ficheiros, ha alguma maneira de tipo dividir o ficheiro em 5 ficheiros .zip e repartir o tamanho igualmente por todos?

    cumps
     
  8. jpaulino

    jpaulino Power Member

    Epá isso nunca tentei ... mas provavelmente até dá.

    Vai testando e eu quando tiver algum tempo vejo se consigo.
     
  9. skullrazor

    skullrazor [email protected] Member

    okay, eu vou ver se invento qualquer coisa xD obrigado por toda a ajuda
     

Partilhar esta Página