Traduzione c# --> vbnet

lunedì 24 gennaio 2005 - 18.51

Elfus Profilo | Newbie

ciao a tutti. Ho letto un articolo interessante su questo sito per zippare dei file con c#. E' da poco che programmo, utilizzo visual basic net 2003. Qualcuno mi può dire come si traduce in visual basic questo listato del vostro articolo? Grazie a tutti quanti.

Listato n°11
' codice omesso
byte[] buffer = new byte[fs.Length]; 'qui mi da l'errore come si dichiara questa cosa qui?
'codice omesso

Cteniza Profilo | Guru

> byte[] buffer = new byte[fs.Length];
Dim buffer() As New Byte(fs.Length)

Elfus Profilo | Newbie

ci avevo provavo anche io così mi dice il seguente errore -->Arrays cannot be declared with New , e mi sottolinea il New

Elfus

Elfus Profilo | Newbie

Nessuno può spiegarmi?

Elfus

pecos81 Profilo | Junior Member

Ciao,
anche io ho avuto questo problema e sono riuscito a trovare il seguente codice (funzionante):

Shared Sub ZipFile(ByVal Filename As String, ByVal strFileToZip As String, ByVal strZippedFile As String, ByVal nCompressionLevel As Integer, ByVal nBlockSize As Integer)
' If (Not System.IO.File.Exists(strFileToZip)) Then
' Throw New System.IO.FileNotFoundException("The specified file " + strFileToZip + "could not be found. Zipping aborted.")
' End If

Dim strmStreamToZip As System.IO.FileStream
strmStreamToZip = New System.IO.FileStream(strFileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)

Dim strmZipFile As System.IO.FileStream
strmZipFile = System.IO.File.Create(strZippedFile)

Dim strmZipStream As ZipOutputStream
strmZipStream = New ZipOutputStream(strmZipFile)

Dim myZipEntry As ZipEntry
myZipEntry = New ZipEntry(Filename)
strmZipStream.PutNextEntry(myZipEntry)
strmZipStream.SetLevel(nCompressionLevel)

Dim abyBuffer(nBlockSize) As Byte
Dim nSize As System.Int32
nSize = strmStreamToZip.Read(abyBuffer, 0, abyBuffer.Length)
strmZipStream.Write(abyBuffer, 0, nSize)

Try
While (nSize < strmStreamToZip.Length)
Dim nSizeRead As Integer
nSizeRead = strmStreamToZip.Read(abyBuffer, 0, abyBuffer.Length)
strmZipStream.Write(abyBuffer, 0, nSizeRead)
nSize = nSize + nSizeRead
nSizeRead = Nothing
End While

Catch Ex As System.Exception
Throw Ex

End Try

strmZipStream.Finish()
strmZipStream.Close()
strmStreamToZip.Close()

strmStreamToZip = Nothing
strmZipFile = Nothing
strmZipStream = Nothing
myZipEntry = Nothing
abyBuffer = Nothing
nSize = Nothing

End Sub

Shared Sub UnzipFile(ByVal directorio As String, _
Optional ByVal zipFic As String = "", _
Optional ByVal eliminar As Boolean = False, _
Optional ByVal renombrar As Boolean = False)
' descomprimir el contenido de zipFic en el directorio indicado.
' si zipFic no tiene la extensión .zip, se entenderá que es un directorio y
' se procesará el primer fichero .zip de ese directorio.
' si eliminar es True se eliminará ese fichero zip después de descomprimirlo.
' si renombrar es True se añadirá al final .descomprimido
' If Not zipFic.ToLower.EndsWith(".zip") Then
' zipFic = Directory.GetFiles(zipFic, "*.zip")(0)
' End If
' si no se ha indicado el directorio, usar el actual
'If directorio = "" Then directorio = "."
'
Dim z As New ZipInputStream(File.OpenRead(zipFic))
Dim theEntry As ZipEntry
'
Do
theEntry = z.GetNextEntry()
If Not theEntry Is Nothing Then
Dim fileName As String = directorio & "\" & Path.GetFileName(theEntry.Name)
'
' dará error si no existe el path
Dim streamWriter As FileStream
Try
streamWriter = File.Create(fileName)
Catch ex As DirectoryNotFoundException
Directory.CreateDirectory(Path.GetDirectoryName(fileName))
streamWriter = File.Create(fileName)
End Try
'
Dim size As Integer
Dim data(2048) As Byte
Do
size = z.Read(data, 0, data.Length)
If (size > 0) Then
streamWriter.Write(data, 0, size)
Else
Exit Do
End If

Cteniza Profilo | Guru

Scusa mi sono fidato troppo del mio intuito:
c#: byte[] buffer = new byte[fs.Length];
vb.net: Dim buffer(fs.Length) As Byte

Elfus Profilo | Newbie

Cteniza mi hai salvato.

Pecos81 grazie per il tuo codice. grazie.





Elfus

peppe2 Profilo | Newbie

penso che il codice giusto sia questo, magari se in futuro(come a me) a qualcuno puo' servire.
Su VB 2008 express funziona.



Shared Sub UnzipFile(ByVal directorio As String, Optional ByVal zipFic As String = "", Optional ByVal eliminar As Boolean = False, Optional ByVal renombrar As Boolean = False)

Dim z As New ZipInputStream(File.OpenRead(zipFic))
Dim theEntry As ZipEntry
'
Do
Try
theEntry = z.GetNextEntry()
Catch
Exit Do
End Try
If Not theEntry Is Nothing Then

'Ricavo il nome della direcory della entry
Dim directoryName As String = My.Computer.FileSystem.GetParentPath(theEntry.Name)
' Ricavo il nome del file della entry
Dim fileName As String = IO.Path.GetFileName(theEntry.Name)
'se è un file e non una directory, quindi il percorsopadre è vuoto
If directoryName = "" Then
Dim streamWriters As FileStream
streamWriters = File.Create(directorio & "\" & fileName)
Dim size As Integer
Dim data(4096) As Byte
Do
size = z.Read(data, 0, data.Length)
If (size > 0) Then
streamWriters.Write(data, 0, size)
Else
Exit Do
End If
Loop
streamWriters.Dispose()
data = Nothing
size = Nothing
Else
'Se è un file e non esiste la directory padre la creo
If IO.Directory.Exists(directorio & "\" & directoryName) = False Then
IO.Directory.CreateDirectory(directorio & "\" & directoryName)
End If
Dim streamWriters As FileStream
streamWriters = File.Create(directorio & "\" & directoryName & "\" & fileName)
Dim size As Integer
Dim data(4096) As Byte
Do
size = z.Read(data, 0, data.Length)
If (size > 0) Then
streamWriters.Write(data, 0, size)
Else
Exit Do
End If
Loop
streamWriters.Dispose()
data = Nothing
size = Nothing
End If

End If

Loop


End Sub
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5