96 lines
4.9 KiB
VB.net
96 lines
4.9 KiB
VB.net
Imports System.IO
|
|
Imports System.IO.Compression
|
|
|
|
<Obsolete("Esta clase es Obsoleta. Utilizar la librería tsZip.")>
|
|
Public Class zip
|
|
Shared Sub ExtraeTodoDeZip(FicheroZIP As IO.MemoryStream, RutaDestino As String, Optional EliminaDirectorioDestino As Boolean = False)
|
|
If RutaDestino.EndsWith("\") = False Then RutaDestino &= "\"
|
|
If IO.Directory.Exists(RutaDestino) And EliminaDirectorioDestino Then
|
|
IO.Directory.Delete(RutaDestino, True)
|
|
End If
|
|
If Not IO.Directory.Exists(RutaDestino) Then tsUtilidades.Utilidades.CreaEstructuraDirectorio(RutaDestino)
|
|
Dim fzip As New ZipArchive(FicheroZIP, ZipArchiveMode.Read)
|
|
For Each entry In fzip.Entries
|
|
Dim sDestino As String = RutaDestino & entry.FullName.Replace("/", "\")
|
|
If Not IO.Directory.Exists(IO.Path.GetDirectoryName(sDestino)) Then
|
|
tsUtilidades.Utilidades.CreaEstructuraDirectorio(IO.Path.GetDirectoryName(sDestino))
|
|
End If
|
|
If entry.FullName.EndsWith("/") = False Then entry.ExtractToFile(sDestino)
|
|
Next
|
|
End Sub
|
|
''' <summary>
|
|
''' Esta función extrae el único fichero que hay dentro de un fichero zip, lo devuelve como array de bytes como retorno de la función, e indica su nombre en un parámetro por referencia.
|
|
''' </summary>
|
|
''' <param name="ficheroZip">Array de bytes conteniendo el fichero zip.</param>
|
|
''' <param name="nombreArchivoDentroZip">Cadena donde se guardará el nombre del fichero que está dentro del fichero zip.</param>
|
|
''' <returns>Para que este método funcione correctamente es imprescindible que el archivo zip tenga dentro un único fichero.</returns>
|
|
Public Shared Function ExtraerFicheroUnicoDeZip(ficheroZip As Byte(), ByRef nombreArchivoDentroZip As String) As Byte()
|
|
Dim sFichero As IO.Stream
|
|
Dim za As ZipArchive = New ZipArchive(New IO.MemoryStream(ficheroZip))
|
|
If za.Entries.Count = 1 Then
|
|
nombreArchivoDentroZip = za.Entries.First.Name
|
|
sFichero = za.Entries.First.Open()
|
|
Else
|
|
Throw New Exception("Se esperaba que el archivo zip tuviera un único fichero dentro, pero la cantidad es distinta. Se aborta la operación.")
|
|
End If
|
|
|
|
Dim ms As New IO.MemoryStream
|
|
sFichero.CopyTo(ms)
|
|
sFichero.Dispose()
|
|
|
|
Return ms.ToArray
|
|
End Function
|
|
''' <summary>
|
|
''' Esta función extrae todos los ficheros que haya en un zip y los devuelve como un diccionario.
|
|
''' </summary>
|
|
''' <param name="ficheroZip">Array de bytes conteniendo el fichero zip.</param>
|
|
''' <returns>Como todo es en memoria, hay que tener cuidado de que los ficheros extraídos quepan en memoria adecuadamente, teniendo en cuenta las posibles restricciones de memoria que el sistema operativo pueda tener para procesos individuales.</returns>
|
|
Public Shared Function ExtraerFicherosDeZip(ficheroZip As Byte()) As Dictionary(Of String, IO.MemoryStream)
|
|
Dim resultado As New Dictionary(Of String, IO.MemoryStream)
|
|
Dim za As ZipArchive = New ZipArchive(New IO.MemoryStream(ficheroZip))
|
|
For Each e In za.Entries
|
|
resultado.Add(e.FullName, e.Open())
|
|
Next
|
|
Return resultado
|
|
End Function
|
|
|
|
'Shared Function ComprimeStream(streamAComprimir As IO.Stream, NombreFicheroAcomprimir As String) As IO.MemoryStream
|
|
' Dim ms As New IO.MemoryStream
|
|
|
|
' Dim fzip As New ZipArchive(ms, ZipArchiveMode.Create)
|
|
' Dim entry As ZipArchiveEntry = fzip.CreateEntryFromFile(NombreFicheroAcomprimir, streamAComprimir)
|
|
' Dim entry As ZipArchiveEntry = fzip.CreateEntry((NombreFicheroAcomprimir, streamAComprimir)
|
|
'End Function
|
|
|
|
'Public Shared Function ComprimirArchivos(dArchivos As Dictionary(Of String, Byte())) As Byte() ' NO ESTÁ PROBADA
|
|
' Dim ms As New MemoryStream
|
|
' Dim za As New ZipArchive(ms, ZipArchiveMode.Create, True)
|
|
' For Each f In dArchivos
|
|
' Dim nf = za.CreateEntry(f.Key)
|
|
' Dim es = nf.Open
|
|
' Dim msa As New MemoryStream(f.Value)
|
|
' msa.CopyTo(es)
|
|
' es.Close()
|
|
' Next
|
|
' Return ms.ToArray
|
|
'End Function
|
|
Public Shared Function ComprimirArchivos(fileContents As Dictionary(Of String, Byte())) As Byte()
|
|
Using memoryStream As New MemoryStream()
|
|
Using archive As New ZipArchive(memoryStream, ZipArchiveMode.Create, True)
|
|
For Each entry In fileContents
|
|
Dim fileName As String = entry.Key
|
|
Dim fileData As Byte() = entry.Value
|
|
Dim zipEntry As ZipArchiveEntry = archive.CreateEntry(fileName)
|
|
|
|
Using entryStream As Stream = zipEntry.Open()
|
|
entryStream.Write(fileData, 0, fileData.Length)
|
|
End Using
|
|
Next
|
|
End Using
|
|
Return memoryStream.ToArray()
|
|
End Using
|
|
End Function
|
|
End Class
|
|
|
|
|