不用WinRar只有asp将网络空间上的文件打包下载
程序员文章站
2022-07-01 23:38:27
<%@ language=vbscript %> <% option explicit %> &l...
<%@ language=vbscript %>
<% option explicit %>
<!--#include file="asptar.asp"-->
<%
response.buffer = true
response.clear
dim co,temp,t,x,i,fsobrowse,thefolder,thesubfolders,filepath,s,ph,objtar
co=0
ph="./upfile" '文件路径 '压缩upfile下的所有文件
set objtar = new tarball
objtar.tarfilename="lvbbs_updatefile.rar" '打包的名称
objtar.path=ph
set fsobrowse=createobject("scripting.filesystemobject")
set thefolder=fsobrowse.getfolder(server.mappath(ph))
set thesubfolders=thefolder.subfolders
for each t in thefolder.files
temp= temp & t.name & "|"
co=co+1
next
for each x in thesubfolders
for each i in x.files
temp= temp & x.name&"/"&i.name&"|"
co=co+1
next
next
if co<1 then
response.write "暂时没有可更新的文件下载"
'objtar.addmemoryfile "sorry.txt","not file!"
else
temp=left(temp,len(temp)-1)
filepath=split(temp,"|")
for s=0 to ubound(filepath)
objtar.addfile server.mappath(ph&"/"&filepath(s))
next
if response.isclientconnected then
objtar.writetar
response.flush
end if
end if
set objtar = nothing
set fsobrowse= nothing
set thefolder = nothing
set thesubfolders = nothing
%>
asptar.asp
<%
' unix tarball creator
' ====================
' author: chris read
' version: 1.0.1
' ====================
'
' this class provides the ability to archive multiple files together into a single
' distributable file called a tarball (the tar actually stands for tape archive).
' these are common unix files which contain uncompressed data.
'
' so what is this useful for? well, it allows you to effectively combine multiple
' files into a single file for downloading. the tar files are readable and extractable
' by a wide variety of tools, including the very widely distributed winzip.
'
' this script can include two types of data in each archive, file data read from a disk,
' and also things direct from memory, like from a string. the archives support files in
' a binary structure, so you can store executable files if you need to, or just store
' text.
'
' this class was developed to assist me with a few projects and has grown with every
' implementation. currently i use this class to tarball xml data for archival purposes
' which allows me to grab 100's of dynamically created xml files in a single download.
'
' there are a small number of properties and methods, which are outlined in the
' accompanying documentation.
'
class tarball
public tarfilename ' resultant tarball filename
public userid ' unix user id
public username ' unix user name
public groupid ' unix group id
public groupname ' unix group name
public permissions ' unix permissions
public blocksize ' block byte size for the tarball (default=512)
public ignorepaths ' ignore any supplied paths for the tarball output
public basepath ' insert a base path with each file
public path
' storage for file information
private objfiles,tmpfilename
private objmemoryfiles
' file list management subs, very basic stuff
public sub addfile(sfilename)
objfiles.add sfilename,sfilename
end sub
public sub removefile(sfilename)
objfiles.remove sfilename
end sub
public sub addmemoryfile(sfilename,scontents)
objmemoryfiles.add sfilename,scontents
end sub
public sub removememoryfile(sfilename)
objmemoryfiles.remove sfilename
end sub
' send the tarball to the browser
public sub writetar()
dim objstream, objinstream, ltemp, afiles
set objstream = server.createobject("adodb.stream") ' the main stream
set objinstream = server.createobject("adodb.stream") ' the input stream for data
objstream.type = 2
objstream.charset = "x-ansi" ' good old extended ascii
objstream.open
objinstream.type = 2
objinstream.charset = "x-ansi"
' go through all files stored on disk first
afiles = objfiles.items
for ltemp = 0 to ubound(afiles)
objinstream.open
objinstream.loadfromfile afiles(ltemp)
objinstream.position = 0
'exportfile afiles(ltemp),objstream,objinstream
tmpfilename =replace(afiles(ltemp),server.mappath(path)&"\","")
exportfile tmpfilename,objstream,objinstream
objinstream.close
next
' now add stuff from memory
afiles = objmemoryfiles.keys
for ltemp = 0 to ubound(afiles)
objinstream.open
objinstream.writetext objmemoryfiles.item(afiles(ltemp))
objinstream.position = 0
exportfile afiles(ltemp),objstream,objinstream
objinstream.close
next
objstream.writetext string(blocksize,chr(0))
' rewind the stream
' remember to change the type back to binary, otherwise the write will truncate
' past the first zero byte character.
objstream.position = 0
objstream.type = 1
' set all the browser stuff
response.addheader "content-disposition","filename=" & tarfilename
response.contenttype = "application/x-tar"
response.binarywrite objstream.read
' close it and go home
objstream.close
set objstream = nothing
set objinstream = nothing
end sub
' build a header for each file and send the file contents
private sub exportfile(sfilename,objoutstream,objinstream)
dim lstart, lsum, ltemp
lstart = objoutstream.position ' record where we are up to
if ignorepaths then
' we ignore any paths prefixed to our filenames
ltemp = instrrev(sfilename,"\")
if ltemp <> 0 then
sfilename = right(sfilename,len(sfilename) - ltemp)
end if
sfilename = basepath & sfilename
end if
' build the header, everything is ascii in octal except for the data
objoutstream.writetext left(sfilename & string(100,chr(0)),100)
objoutstream.writetext "100" & right("000" & oct(permissions),3) & " " & chr(0) 'file mode
objoutstream.writetext right(string(6," ") & cstr(userid),6) & " " & chr(0) 'uid
objoutstream.writetext right(string(6," ") & cstr(groupid),6) & " " & chr(0) 'gid
objoutstream.writetext right(string(11,"0") & oct(objinstream.size),11) & chr(0) 'size
objoutstream.writetext right(string(11,"0") & oct(datediff("s","1/1/1970 10:00",now())),11) & chr(0) 'mtime (number of seconds since 10am on the 1st january 1970 (10am correct?)
objoutstream.writetext " 0" & string(100,chr(0)) 'chksum, type flag and link name, write out all blanks so that the actual checksum will get calculated correctly
objoutstream.writetext "ustar " & chr(0) 'magic and version
objoutstream.writetext left(username & string(32,chr(0)),32) 'uname
objoutstream.writetext left(groupname & string(32,chr(0)),32) 'gname
objoutstream.writetext " 40 " & string(4,chr(0)) 'devmajor, devminor
objoutstream.writetext string(167,chr(0)) 'prefix and leader
objinstream.copyto objoutstream ' send the data to the stream
if (objinstream.size mod blocksize) > 0 then
objoutstream.writetext string(blocksize - (objinstream.size mod blocksize),chr(0)) 'padding to the nearest block byte boundary
end if
' calculate the checksum for the header
lsum = 0
objoutstream.position = lstart
for ltemp = 1 to blocksize
lsum = lsum + (asc(objoutstream.readtext(1)) and &hff&)
next
' insert it
objoutstream.position = lstart + 148
objoutstream.writetext right(string(7,"0") & oct(lsum),7) & chr(0)
' move to the end of the stream
objoutstream.position = objoutstream.size
end sub
' start everything off
private sub class_initialize()
set objfiles = server.createobject("scripting.dictionary")
set objmemoryfiles = server.createobject("scripting.dictionary")
blocksize = 512
permissions = 438 ' unix 666
userid = 0
username = "root"
groupid = 0
groupname = "root"
ignorepaths = false
basepath = ""
tarfilename = "new.tar"
end sub
private sub class_terminate()
set objmemoryfiles = nothing
set objfiles = nothing
end sub
end class
%>
上一篇: 05替换空格