#console 
#include "zlib.sbp"

Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA"(lpszCallerName As BytePtr,dwAccessType As Long,_ 
lpszProxyName As BytePtr,lpszProxyBypass As BytePtr,dwFlags As Long) As Long 
Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA"(hHttpRequest As Long,dwInfoLevel As Long,_ 
lpvBuffer As BytePtr,ByRef lpdwBufferLength As Long,ByRef lpdwIndex As Long) As Long 
Declare Function InternetCloseHandle Lib "wininet.dll"(hInternetHandle As Long) As Long 
Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (hInternetSession As Long,_ 
sUrl As BytePtr,sHeaders As Long,lHeadersLength As Long,lFlags As Long,lContext As Long) As Long 
Declare Function InternetReadFile Lib "wininet.dll" (hFile As Long, sBuffer As BytePtr,_ 
lNumBytesToRead As Long,lNumberOfBytesRead As Long) As Long 

Const HTTP_QUERY_CONTENT_TYPE=1 
Const HTTP_QUERY_CONTENT_LENGTH=5 
Const HTTP_QUERY_LAST_MODIFIED=11 

Const UA = "Monazilla/1.00"

Sub Download(url As BytePtr, localfile As BytePtr, header As *Byte) 
	Dim hOpen As HANDLE, hUrl As HANDLE,hFile As HANDLE 
	Dim buffer As BytePtr 
	Dim Size As Long 
	Dim a As Long 
	Dim dwReadSize As DWord, dwWriteSize As DWord 
	Dim TotalReadSize As DWord 

	Print "接続待ち -> ";MakeStr(url) 
	Size = 33333 
	buffer=calloc(Size) 
	hOpen=InternetOpen(UA,0,NULL,NULL,0) 
	hUrl=InternetOpenUrl(hOpen,url,header,-1,0,0) 
	HttpQueryInfo(url,HTTP_QUERY_CONTENT_LENGTH,buffer,Size,a) 
	Print "ファイルサイズ -> ";MakeStr(buffer) 
	hFile = CreateFile(localfile, GENERIC_WRITE, 0, ByVal NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0) 
	If hFile = INVALID_HANDLE_VALUE Then 
		Print "CreateFile失敗" 
		Goto *goot 
	End If 
	Print "DL開始" 
	Do 
	InternetReadFile(hUrl, buffer, Size, VarPtr(dwReadSize)) 
	WriteFile(hFile, buffer, dwReadSize,VarPtr(dwWriteSize), ByVal NULL) 
		If dwReadSize <> dwWriteSize Then Print "書き込みエラー？" 
		TotalReadSize += dwReadSize 
		Print TotalReadSize;"Byte" 
	Loop While (dwReadSize) 
	Print "完了" 
	*goot
	CloseHandle(hFile) 
	InternetCloseHandle(hUrl) 
	InternetCloseHandle(hOpen) 
	free(buffer) 
End Sub


Const OUTBUFSIZ = 1024
Const INBUFSIZ = 1024
Sub Decompress(gzipfile As BytePtr, datfile As BytePtr) 
	Dim hFin As HANDLE, hFout As HANDLE
	Dim outbuf[OUTBUFSIZ] As Byte
	Dim inbuf[INBUFSIZ] As Byte
	Dim count As Long, status As Long
	Dim r As DWord, w As DWord

	hFin  = CreateFile(gzipfile, GENERIC_READ , 0, ByVal NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0) 
	hFout = CreateFile(datfile , GENERIC_WRITE, 0, ByVal NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0) 

	If hFin = INVALID_HANDLE_VALUE or hFout = INVALID_HANDLE_VALUE Then
		Print "ERR CreateFile"
		Exit Sub
	End If
	ReadFile(hFin, inbuf, 10, VarPtr(r), ByVal NULL)
    ' すべてのメモリ管理をライブラリに任せる 
    z.zalloc = Z_NULL
    z.zfree = Z_NULL
    z.opaque = Z_NULL

    ' 初期化 
    z.next_in = Z_NULL
    z.avail_in = 0
    If inflateInit2_(VarPtr(z), -MAX_WBITS, "1.2.3", SizeOf(z_stream)) <> Z_OK Then
		Print "ERR inflateInit2_" 
        Goto *CREANUP
    End If

    z.next_out = outbuf        ' 出力ポインタ 
    z.avail_out = OUTBUFSIZ    ' 出力バッファ残量 
    status = Z_OK

    While status <> Z_STREAM_END
        If z.avail_in = 0 Then ' 入力残量がゼロになれば 
            z.next_in = inbuf  ' 入力ポインタを元に戻す 
			ReadFile(hFin, inbuf, INBUFSIZ, VarPtr(r), ByVal NULL)
            z.avail_in =  r' データを読む 
        End If
        status = inflate(VarPtr(z), Z_NO_FLUSH) ' 展開 
        If status = Z_STREAM_END Then Exit While ' 完了 
        If status <> Z_OK Then   ' エラー 
            Print "ERR inflate";MakeStr(z.msg)
            Goto *CREANUP
        End If

        If z.avail_out = 0 Then ' 出力バッファが尽きれば 
            ' まとめて書き出す 
			WriteFile(hFout,outbuf, OUTBUFSIZ, VarPtr(w), ByVal NULL)
            If w <> OUTBUFSIZ Then
                Print "ERR WriteFile"
                Goto *CREANUP
            End If
            z.next_out = outbuf ' 出力ポインタを元に戻す 
            z.avail_out = OUTBUFSIZ ' 出力バッファ残量を元に戻す 
        End If
    Wend

    ' 残りを吐き出す 
	count = OUTBUFSIZ - z.avail_out
    If count <>  0 Then
		WriteFile( hFout,outbuf, count, VarPtr(w), ByVal NULL)
        If w <> count Then
            Print "ERR WriteFile"
            Goto *CREANUP
        End If
    End If

    ' 後始末 
    If inflateEnd(VarPtr(z)) <> Z_OK Then
        Print "ERR inflateEnd"
        Goto *CREANUP
    End If

*CREANUP
CloseHandle(hFin)
CloseHandle(hFout)
End Sub

'テスト
Dim z As z_stream
Dim i As String 
Download("http://pc12.2ch.net/tech/dat/1264805066.dat", "test.gzip", Ex"Accept-Encoding: gzip\r\n\r\n") 
Decompress("test.gzip", "test.dat")
Input i 
