批处理之家's Archiver

nwm310 发表于 2016-9-22 21:10

看rar檔裡面的圖片

Win10測試 OK

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

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

全螢幕顯示圖片,點圖片會顯示下一張,可用 Alt + F4 關掉[code]$dllfile = gi unrar64.dll -ea stop

Add-Type -Type @"
using System;
using System.Runtime.InteropServices;

namespace PS
{
  public class Unrar
  {
        [StructLayout(LayoutKind.Sequential)]
                public struct RAROpenArchiveDataEx
        {
                [MarshalAs(UnmanagedType.LPStr)]
                public string ArcName;

                [MarshalAs(UnmanagedType.LPWStr)]
                public string ArcNameW;

                public uint OpenMode;
                public uint OpenResult;

                [MarshalAs(UnmanagedType.LPStr)]
                public string CmtBuf;

                public uint CmtBufSize;
                public uint CmtSize;
                public uint CmtState;
                public uint Flags;

                public UNRARCallback Callback;
                    public uint UserData;

                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)]
                public uint[] Reserved;

        }


        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
                public struct RARHeaderDataEx
        {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
                public string ArcName;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
                public string ArcNameW;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=512)]
                public string FileName;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
                public string FileNameW;

                public uint Flags;
                public uint PackSize;
                public uint PackSizeHigh;
                public uint UnpSize;
                public uint UnpSizeHigh;
                public uint HostOS;
                public uint FileCRC;
                public uint FileTime;
                public uint UnpVer;
                public uint Method;
                public uint FileAttr;

                [MarshalAs(UnmanagedType.LPStr)]
                public string CmtBuf;

                public uint CmtBufSize;
                public uint CmtSize;
                public uint CmtState;

                public uint DictSize;
                public uint HashType;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
                public string Hash;

                public uint RedirType;

                [MarshalAs(UnmanagedType.LPWStr)]
                public string RedirName;

                public uint RedirNameSize;
                public uint DirTarget;

                [MarshalAs(UnmanagedType.ByValArray, SizeConst=994)]
                public uint[] Reserved;
        }

        public delegate int UNRARCallback(uint msg, int UserData, IntPtr p1, int p2);

        //============================================================================================

        [DllImport(@"$dllfile")]
        public static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);

        [DllImport(@"$dllfile")]
        public static extern int RARCloseArchive(IntPtr hArcData);

        [DllImport(@"$dllfile")]
        public static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);

        [DllImport(@"$dllfile")]
        public static extern int RARProcessFileW(IntPtr hArcData, int operation,
                        [MarshalAs(UnmanagedType.LPWStr)] string destPath,
                        [MarshalAs(UnmanagedType.LPWStr)] string destName );

  }
}

"@

#=======================================================================================
$sb={
        param(        [uint32]$msg,
                [int32]$UserData,
                [IntPtr]$p1,
                [int32]$p2
        )

        switch ($msg)
        {
                1{
                        [System.Runtime.InteropServices.Marshal]::Copy($p1,$buffer,$bufferPos,$p2)
                        $script:bufferPos+=$p2
                        return 1
                }

                3{
                        $fn = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($p1)
                          if ($p2 -eq 0)  {write-host "沒找到 $fn";return -1}
                          if ($p2 -eq 1)  {write-host "找到 $fn";return 1}
                }

                4{       
                        $pass = read-host '請輸入密碼'
                        $pass = [System.Text.Encoding]::Unicode.GetBytes($pass)
                        [System.Runtime.InteropServices.Marshal]::Copy($pass,0,$p1,$pass.length)
                        return 1
                }
        }
}
#=======================================================================================
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$form = new-object Windows.Forms.Form
$form.FormBorderStyle = [Windows.Forms.FormBorderStyle]::None
$form.WindowState=[Windows.Forms.FormWindowState]::Maximized


$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Dock="Fill"
$pictureBox.SizeMode = [Windows.Forms.PictureBoxSizeMode]::Zoom

$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
#===============================================================
$file = @(dir *.rar)[0]
if ($file -eq $null) {write-host '沒找到 rar';pause;exit}

#設定 $open
$open=new-object PS.Unrar+RAROpenArchiveDataEx
$open.ArcNameW=$file
$open.OpenMode=1
$open.Reserved=@(0)*28
$open.Callback=$sb

#開始 open
$handle=[PS.Unrar]::RAROpenArchiveEx([ref]$open)
if ( $open.OpenResult){write-host 'ERROR open';pause;exit}

#==============================================================
$nextP={
        #讀取 header
        $header = new-object PS.Unrar+RARHeaderDataEx

        while ($true) {
                $result = [PS.Unrar]::RARReadHeaderEx($handle,[ref]$header)
                #header讀取錯誤 or 所有header都讀完了
                if ( $result ){[PS.Unrar]::RARCloseArchive($handle);stop-process -Id $PID}

                #找 jpg png bmp 。 如果不是,就跳到下一個header
                if ($header.FileNameW -match '\.(jpg|png|bmp)$') { break} else {
                [PS.Unrar]::RARProcessFileW($handle,0,$null,$null)}

        }


        #解壓縮到$buffer
        $script:buffer=new-object byte[]($header.UnpSize)
        $script:bufferPos=0
        $result=[PS.Unrar]::RARProcessFileW($handle,1,$null,$null)

        #解壓縮錯誤
        if ( $result ){[PS.Unrar]::RARCloseArchive($handle);stop-process -Id $PID}

       
        $ms=new-object IO.MemoryStream($buffer,0,$buffer.length)
        $img = [System.Drawing.Image]::FromStream($ms)
        $pictureBox.Image = $img
        $ms.close()

}
#===================================================================================

$pictureBox.Add_Click($nextP)

& $nextP

[void]$form.ShowDialog()

[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.