Board logo

标题: [原创代码] 看rar檔裡面的圖片 [打印本页]

作者: nwm310    时间: 2016-9-22 21:10     标题: 看rar檔裡面的圖片

Win10測試 OK

需要unrar64.dll
http://www.rarlab.com/rar/UnRARDLL.exe

把 unrar64.dll 、 rar檔 放到 ps1 旁邊
只會顯示第一個壓縮檔裡的圖片

全螢幕顯示圖片,點圖片會顯示下一張,可用 Alt + F4 關掉
  1. $dllfile = gi unrar64.dll -ea stop
  2. Add-Type -Type @"
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace PS
  6. {
  7.   public class Unrar
  8.   {
  9. [StructLayout(LayoutKind.Sequential)]
  10. public struct RAROpenArchiveDataEx
  11. {
  12. [MarshalAs(UnmanagedType.LPStr)]
  13. public string ArcName;
  14. [MarshalAs(UnmanagedType.LPWStr)]
  15. public string ArcNameW;
  16. public uint OpenMode;
  17. public uint OpenResult;
  18. [MarshalAs(UnmanagedType.LPStr)]
  19. public string CmtBuf;
  20. public uint CmtBufSize;
  21. public uint CmtSize;
  22. public uint CmtState;
  23. public uint Flags;
  24. public UNRARCallback Callback;
  25.     public uint UserData;
  26. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)]
  27. public uint[] Reserved;
  28. }
  29. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  30. public struct RARHeaderDataEx
  31. {
  32. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
  33. public string ArcName;
  34. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
  35. public string ArcNameW;
  36. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
  37. public string FileName;
  38. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
  39. public string FileNameW;
  40. public uint Flags;
  41. public uint PackSize;
  42. public uint PackSizeHigh;
  43. public uint UnpSize;
  44. public uint UnpSizeHigh;
  45. public uint HostOS;
  46. public uint FileCRC;
  47. public uint FileTime;
  48. public uint UnpVer;
  49. public uint Method;
  50. public uint FileAttr;
  51. [MarshalAs(UnmanagedType.LPStr)]
  52. public string CmtBuf;
  53. public uint CmtBufSize;
  54. public uint CmtSize;
  55. public uint CmtState;
  56. public uint DictSize;
  57. public uint HashType;
  58. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
  59. public string Hash;
  60. public uint RedirType;
  61. [MarshalAs(UnmanagedType.LPWStr)]
  62. public string RedirName;
  63. public uint RedirNameSize;
  64. public uint DirTarget;
  65. [MarshalAs(UnmanagedType.ByValArray, SizeConst=994)]
  66. public uint[] Reserved;
  67. }
  68. public delegate int UNRARCallback(uint msg, int UserData, IntPtr p1, int p2);
  69.         //============================================================================================
  70. [DllImport(@"$dllfile")]
  71. public static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);
  72. [DllImport(@"$dllfile")]
  73. public static extern int RARCloseArchive(IntPtr hArcData);
  74. [DllImport(@"$dllfile")]
  75. public static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);
  76. [DllImport(@"$dllfile")]
  77. public static extern int RARProcessFileW(IntPtr hArcData, int operation,
  78. [MarshalAs(UnmanagedType.LPWStr)] string destPath,
  79. [MarshalAs(UnmanagedType.LPWStr)] string destName );
  80.   }
  81. }
  82. "@
  83. #=======================================================================================
  84. $sb={
  85. param( [uint32]$msg,
  86. [int32]$UserData,
  87. [IntPtr]$p1,
  88. [int32]$p2
  89. )
  90. switch ($msg)
  91. {
  92. 1{
  93. [System.Runtime.InteropServices.Marshal]::Copy($p1,$buffer,$bufferPos,$p2)
  94. $script:bufferPos+=$p2
  95. return 1
  96. }
  97. 3{
  98. $fn = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($p1)
  99.   if ($p2 -eq 0)  {write-host "沒找到 $fn";return -1}
  100.   if ($p2 -eq 1)  {write-host "找到 $fn";return 1}
  101. }
  102. 4{
  103. $pass = read-host '請輸入密碼'
  104. $pass = [System.Text.Encoding]::Unicode.GetBytes($pass)
  105. [System.Runtime.InteropServices.Marshal]::Copy($pass,0,$p1,$pass.length)
  106. return 1
  107. }
  108. }
  109. }
  110. #=======================================================================================
  111. [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
  112. $form = new-object Windows.Forms.Form
  113. $form.FormBorderStyle = [Windows.Forms.FormBorderStyle]::None
  114. $form.WindowState=[Windows.Forms.FormWindowState]::Maximized
  115. $pictureBox = new-object Windows.Forms.PictureBox
  116. $pictureBox.Dock="Fill"
  117. $pictureBox.SizeMode = [Windows.Forms.PictureBoxSizeMode]::Zoom
  118. $form.controls.add($pictureBox)
  119. $form.Add_Shown( { $form.Activate() } )
  120. #===============================================================
  121. $file = @(dir *.rar)[0]
  122. if ($file -eq $null) {write-host '沒找到 rar';pause;exit}
  123. #設定 $open
  124. $open=new-object PS.Unrar+RAROpenArchiveDataEx
  125. $open.ArcNameW=$file
  126. $open.OpenMode=1
  127. $open.Reserved=@(0)*28
  128. $open.Callback=$sb
  129. #開始 open
  130. $handle=[PS.Unrar]::RAROpenArchiveEx([ref]$open)
  131. if ( $open.OpenResult){write-host 'ERROR open';pause;exit}
  132. #==============================================================
  133. $nextP={
  134. #讀取 header
  135. $header = new-object PS.Unrar+RARHeaderDataEx
  136. while ($true) {
  137. $result = [PS.Unrar]::RARReadHeaderEx($handle,[ref]$header)
  138. #header讀取錯誤 or 所有header都讀完了
  139. if ( $result ){[PS.Unrar]::RARCloseArchive($handle);stop-process -Id $PID}
  140. #找 jpg png bmp 。 如果不是,就跳到下一個header
  141. if ($header.FileNameW -match '\.(jpg|png|bmp)$') { break} else {
  142. [PS.Unrar]::RARProcessFileW($handle,0,$null,$null)}
  143. }
  144. #解壓縮到$buffer
  145. $script:buffer=new-object byte[]($header.UnpSize)
  146. $script:bufferPos=0
  147. $result=[PS.Unrar]::RARProcessFileW($handle,1,$null,$null)
  148. #解壓縮錯誤
  149. if ( $result ){[PS.Unrar]::RARCloseArchive($handle);stop-process -Id $PID}
  150. $ms=new-object IO.MemoryStream($buffer,0,$buffer.length)
  151. $img = [System.Drawing.Image]::FromStream($ms)
  152. $pictureBox.Image = $img
  153. $ms.close()
  154. }
  155. #===================================================================================
  156. $pictureBox.Add_Click($nextP)
  157. & $nextP
  158. [void]$form.ShowDialog()
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2