免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1160 | 回复: 0
打印 上一主题 下一主题

利用cURL实现单个文件分多段同时下载,支持断点续传 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-01 08:19 |只看该作者 |倒序浏览
下面是代码:(转自CU)
(俺是新手,高手见笑)
用法:./mycurl url
#!/bin/bash
######################################################################################################
#
# Script for curl to support resumable multi-part download.
#
url=$1
exename=`basename $0`
# How many "parts" will the target file be divided into?
declare -i parts=5
read -ep "Please input the target directory: " targetdir
read -ep "Please input the outfile name: " outfile
[ -z "$targetdir" ] && targetdir="./"
cd $targetdir||exit 2
[ -z "$outfile" ] && outfile=`basename $1`
length=`curl -s -I $url|grep Content-Length|sed s/[^0-9]//g`
#length=`curl -s -I $url|grep Content-Length|awk '{print $2}'|sed s/[^0-9]//`
if [ -z "$length" ]; then
        echo "cann't get the length of the target file"
        exit 1
fi
let "length = $length"
declare -i lsession=$(($length/$parts))
finished="false"
while true;
do
declare -i curr=0
for (( i=1; i> $outfile$i &
                fi
        else
                if [ $offset -lt $(($length-$(($lsession*$(($parts-1)))))) ]; then
                        curl -r $(($curr+$offset))- $url >> $outfile$i &
                fi
        fi
               
        curr=$(($curr+$lsession))                        
done
#To judge if all curl threads have terminated.
while true;
do
        hasCurl=`ps -A -o ppid,cmd|grep $$|grep -v "$exename"|grep -v "grep"|grep curl`
        [ -z "$hasCurl" ] && break
        sleep 10
done
finished="true"
for (( i=1; i> $outfile
        rm $outfile$i
done
echo "Done."


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5591/showart_268964.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP