elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Comprimir en ZIP con Visual Basic?
0 Usuarios y 2 Visitantes están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: Comprimir en ZIP con Visual Basic?  (Leído 10,124 veces)
ssccaann43 ©


Desconectado Desconectado

Mensajes: 792


¬¬


Ver Perfil
Re: Comprimir en ZIP con Visual Basic?
« Respuesta #10 en: 4 Agosto 2009, 21:05 pm »

Hola estuve mirando el codigo de XcryptOR por lo que vi tiene unos problemas con los ByVal por ese motivo hay que compilarlo en p-code

les quite el byval en algunos WriteFile y CopyMemory y ahora no hace falta compilarlo en p-code


Código
  1. Option Explicit
  2. Private Declare Function CreateFile Lib "Kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  3. Private Declare Function ReadFile Lib "Kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
  4. Private Declare Function WriteFile Lib "Kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
  5. Private Declare Function CloseHandle Lib "Kernel32" (ByVal hObject As Long) As Long
  6. Private Declare Function GlobalAlloc Lib "Kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  7. Private Declare Function GlobalFree Lib "Kernel32" (ByVal hMem As Long) As Long
  8. Private Declare Function GetFileSize Lib "Kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
  9. Private Declare Sub ZeroMemory Lib "Kernel32" Alias "RtlZeroMemory" (dest As Any, ByVal numbytes As Long)
  10. Private Declare Function SetFilePointer Lib "Kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
  11. Private Declare Sub GetSystemTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME)
  12. Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  13.  
  14. Private Const FILE_BEGIN = 0
  15. Private Const GENERIC_READ = &H80000000
  16. Private Const GENERIC_WRITE = &H40000000
  17. Private Const FILE_SHARE_READ = &H1
  18. Private Const CREATE_ALWAYS = 2
  19. Private Const OPEN_EXISTING = 3
  20. Private Const INVALID_HANDLE_VALUE = -1
  21. Private Const GMEM_FIXED = &H0
  22. Private Const GMEM_ZEROINIT = &H40
  23. Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
  24.  
  25. Private Type LOCAL_FILE_HEADER
  26. Signature As Long 'Firma &H04034b50
  27. ver_needed As Integer 'Version minima de software necesaria para extraer el archivo
  28. Flags As Integer 'Opciones
  29. method As Integer 'Metodo de compresion
  30. lastmod_time As Integer 'Tiempo de ultima modificacion
  31. lastmod_date As Integer 'Fecha de ultima modificacion
  32. crcLO As Integer 'CRC del file
  33. crcHI As Integer
  34. compressed_sizeLO As Integer 'Tamaño de file comprimido
  35. compressed_sizeHI As Integer
  36. uncompressed_sizeLO As Integer 'Tamaño del file sin comprimir
  37. uncompressed_sizeHI As Integer
  38. filename_length As Integer 'Longitud del nombre del Archivo
  39. extra_length As Integer 'Longitud de "InFormacion Adicional" ¿?
  40. End Type
  41.  
  42. Private Type CENTRAL_DIRECTORY_STRUCTURE
  43. Signature As Long 'FIRMA &H02014b50
  44. made_by As Integer 'Indica SO y version de software donde se comprimio el file
  45. ver_needed As Integer 'Version minima de software necesaria para extraer el archivo
  46. Flags As Integer 'Opciones
  47. method As Integer 'Metodo de compresion
  48. lastmod_time As Integer 'Tiempo de ultima modificacion
  49. lastmod_date As Integer 'Fecha de ultima modificacion
  50. crc As Long 'CRC del file
  51. compressed_size As Long 'Tamaño de file comprimido
  52. uncompressed_size As Long 'Tamaño del file sin comprimir
  53. filename_length As Integer 'Longitud del nombre del Archivo
  54. extra_length As Integer 'Longitud de "InFormacion Adicional" ¿?
  55. comment_length As Integer 'Longitud de los comentarios
  56. disk_nums As Integer 'El número del disco por el cual este archivo comienza ¿?
  57. internal_attr As Integer 'Opciones entre ellas: Si el file tiene datos ASCII(texto) o Binarios
  58. external_attrLO As Integer 'Opciones entre ellas: Tipo de Sistema de Archivos
  59. external_attrHI As Integer '
  60. local_offs As Long 'N° de Byte donde comienza el correspondiente
  61. 'LOCAL_FILE_HEADER de esta struct CENTRAL_DIRECTORY_STRUCTURE
  62. End Type
  63.  
  64. Private Type END_CENTRAL_DIR
  65. Signature As Long 'FIrma &H06054b50
  66. disk_nums As Integer '"El número de este disco, que contiene el expediente de extremo central del directorio" ¿?
  67. disk_dirstart As Integer '"El número del disco en el cual el directorio central comienza" ¿?
  68. disk_dir_entries As Integer 'El número de entradas en el central directory en este disco
  69. dir_entries As Integer 'El número total de archivos en el zipfile
  70. dir_size As Long 'El tamaño (en bytes) de la o las CENTRAL_DIRECTORY_STRUCTURE que contenga el zip
  71. dir_offs As Long 'N° de Byte donde comienza la CENTRAL_DIRECTORY_STRUCTURE o la primera CENTRAL_DIRECTORY_STRUCTURE
  72. 'si es que hay más de una
  73. comment_length As Integer 'Longitud de los Comentarios
  74. End Type
  75.  
  76. Private Type SYSTEMTIME
  77. wYear As Integer
  78. wMonth As Integer
  79. wDayOfWeek As Integer
  80. wDay As Integer
  81. wHour As Integer
  82. wMinute As Integer
  83. wSecond As Integer
  84. wMilliseconds As Integer
  85. End Type
  86.  
  87. Private Type HL_DWORD
  88. LoWord As Integer
  89. HiWord As Integer
  90. End Type
  91.  
  92. Private CRCTable(256) As Long
  93.  
  94. Private Sub SetCRCTable()
  95. 'Code CRC32 de www.vbaccelerator.com
  96. On Error Resume Next
  97. Dim dwPolynomial As Long, dwCrc As Long, I As Integer, j As Integer
  98. dwPolynomial = &HEDB88320
  99.  
  100. For I = 0 To 255
  101. dwCrc = I
  102. For j = 8 To 1 Step -1
  103. If (dwCrc And 1) Then
  104. dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
  105. dwCrc = dwCrc Xor dwPolynomial
  106. Else
  107. dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
  108. End If
  109. Next
  110. CRCTable(I) = dwCrc
  111. Next
  112. End Sub
  113.  
  114. Private Function GetCRC32(Buffer As String) As Long
  115. 'Code CRC32 de www.vbaccelerator.com
  116. On Error Resume Next
  117. Dim crc As Long, I As Long, iLookup As Integer
  118.  
  119. crc = &HFFFFFFFF
  120.  
  121. For I = 1 To Len(Buffer)
  122. iLookup = (crc And &HFF) Xor Asc(Mid(Buffer, I, 1))
  123. crc = ((crc And &HFFFFFF00) \ &H100) And 16777215
  124. crc = crc Xor CRCTable(iLookup)
  125. Next
  126.  
  127. GetCRC32 = Not (crc)
  128. End Function
  129.  
  130. Public Function Zipea(ffile As String, fzip As String, fname As String) As Boolean
  131. On Error Resume Next
  132. Dim lfh As LOCAL_FILE_HEADER
  133. Dim cds As CENTRAL_DIRECTORY_STRUCTURE
  134. Dim ecd As END_CENTRAL_DIR
  135. Dim st As SYSTEMTIME
  136. Dim File As String, FPtr As Long
  137. Dim sz As Long, Dw As Long, o As Long
  138. Dim hFile As Long, hZip As Long
  139. Dim HL As HL_DWORD
  140. Dim CRC32 As Long
  141.  
  142. o = 0
  143.  
  144. hFile = CreateFile(ffile, GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, 0, 0)
  145. If (hFile = INVALID_HANDLE_VALUE) Then Zipea = False: Exit Function
  146.  
  147. hZip = CreateFile(fzip, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, CREATE_ALWAYS, 0, 0)
  148. If (hZip = INVALID_HANDLE_VALUE) Then CloseHandle (hFile): Zipea = False: Exit Function
  149.  
  150. ZeroMemory ByVal lfh, Len(lfh)
  151. ZeroMemory ByVal cds, Len(cds)
  152. ZeroMemory ByVal ecd, Len(ecd)
  153.  
  154. Call GetSystemTime(st)
  155. If (st.wHour > 12) Then st.wHour = st.wHour - 12
  156.  
  157. sz = GetFileSize(hFile, 0)
  158.  
  159. lfh.Signature = &H4034B50
  160. lfh.ver_needed = 10
  161. lfh.Flags = 0
  162. lfh.method = 0
  163. lfh.lastmod_time = (st.wHour) * (2 ^ 11) Or (st.wMinute * (2 ^ 5)) Or (st.wSecond / 2)
  164. lfh.lastmod_date = ((st.wYear - 1980) * (2 ^ 9)) Or (st.wMonth * (2 ^ 5)) Or (st.wDay)
  165.  
  166. CopyMemory HL, sz, 4
  167.  
  168. lfh.uncompressed_sizeHI = HL.HiWord And &HFFFF
  169. lfh.uncompressed_sizeLO = HL.LoWord And &HFFFF
  170. lfh.compressed_sizeHI = HL.HiWord And &HFFFF
  171. lfh.compressed_sizeLO = HL.LoWord And &HFFFF
  172. lfh.filename_length = Len(fname)
  173. lfh.extra_length = 0
  174.  
  175. cds.Signature = &H2014B50
  176. cds.made_by = 20 'MSDOS=0, PKZIP 2.0 =20
  177. cds.ver_needed = 10
  178. cds.Flags = 0
  179. cds.method = 0
  180. cds.lastmod_time = (st.wHour) * (2 ^ 11) Or (st.wMinute * (2 ^ 5)) Or (st.wSecond / 2)
  181. cds.lastmod_date = ((st.wYear - 1980) * (2 ^ 9)) Or (st.wMonth * (2 ^ 5)) Or (st.wDay)
  182. cds.compressed_size = sz
  183. cds.uncompressed_size = sz
  184. cds.filename_length = Len(fname)
  185. cds.extra_length = 0
  186. cds.comment_length = 0
  187. cds.disk_nums = 0
  188. cds.local_offs = 0
  189. cds.internal_attr = 0 'Datos Binarios
  190. cds.external_attrLO = &H20 'FAT_32 (&H20=32)
  191. cds.external_attrHI = &H0
  192.  
  193.  
  194. Call SetFilePointer(hFile, 0, 0, FILE_BEGIN)
  195. FPtr = GlobalAlloc(GPTR, sz)
  196. If (FPtr = 0) Then Zipea = False: GoTo Cierra
  197.  
  198. Call ReadFile(hFile, ByVal FPtr, sz, Dw, ByVal 0)
  199. If (Dw = 0) Then Zipea = False: GoTo Cierra
  200.  
  201. File = Space$(Dw)
  202. CopyMemory ByVal File, ByVal FPtr, Dw
  203.  
  204. Call SetCRCTable
  205.  
  206. CRC32 = GetCRC32(File)
  207.  
  208. CopyMemory HL, CRC32, 4
  209.  
  210. lfh.crcLO = HL.LoWord And &HFFFF
  211. lfh.crcHI = HL.HiWord And &HFFFF
  212.  
  213. cds.crc = CRC32
  214.  
  215. Call WriteFile(hZip, lfh, Len(lfh), Dw, ByVal 0&)
  216. Call WriteFile(hZip, ByVal fname, Len(fname), Dw, ByVal 0&)
  217. Call WriteFile(hZip, ByVal File, sz, Dw, ByVal 0&)
  218.  
  219. GlobalFree (FPtr)
  220. o = o + (Len(lfh) + Len(fname) + sz)
  221.  
  222. ecd.dir_offs = o
  223.  
  224. Call WriteFile(hZip, cds, Len(cds), Dw, ByVal 0&)
  225. Call WriteFile(hZip, ByVal fname, Len(fname), Dw, ByVal 0&)
  226. o = o + (Len(cds) + Len(fname))
  227.  
  228. ecd.Signature = &H6054B50
  229. ecd.disk_nums = 0
  230. ecd.disk_dirstart = 0
  231. ecd.disk_dir_entries = 1
  232. ecd.dir_entries = 1
  233. ecd.dir_size = o - ecd.dir_offs
  234. ecd.comment_length = 0
  235. Call WriteFile(hZip, ecd, Len(ecd), Dw, ByVal 0&)
  236.  
  237. Zipea = True
  238. Cierra:
  239. CloseHandle (hFile): CloseHandle (hZip)
  240. End Function
  241.  
  242.  
  243.  
  244. Private Sub Form_Load()
  245. If Zipea("C:\SearchDesckTop.exe", "C:\nombrefile.zip", "SearchDesckTop.exe") = True Then MsgBox "Compresion de Archivo Exitosa" Else Beep
  246. End Sub
  247.  

Saludos



Quedó muy bueno. Excelente aporte!


En línea

- Miguel Núñez
Todos tenemos derechos a ser estupidos, pero algunos abusan de ese privilegio...
"I like ^TiFa^"
ssccaann43 ©


Desconectado Desconectado

Mensajes: 792


¬¬


Ver Perfil
Re: Comprimir en ZIP con Visual Basic?
« Respuesta #11 en: 4 Agosto 2009, 21:07 pm »

mmm... Ya estaba por preguntarme lo mismo que todos... Porque su tamaño sigue siendo el mismo...


En línea

- Miguel Núñez
Todos tenemos derechos a ser estupidos, pero algunos abusan de ese privilegio...
"I like ^TiFa^"
Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Re: Comprimir en ZIP con Visual Basic?
« Respuesta #12 en: 4 Agosto 2009, 21:58 pm »

 >:( Planet Source Code esta caido >:(

Habia un codigo sobre GZIP muy bueno :(

Bueno, seria aplicar la compression GZIP y modificar esta 'propiedad' por uno que todavia no se cual seria :xD
Código:
LOCAL_FILE_HEADER.method

No recuerdo si era GZIP la compression o algo de LW :-\
« Última modificación: 4 Agosto 2009, 22:00 pm por Karcrack » En línea

Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines