Qt -Ftp下载之修改文件属性(修改时间,访问时间)
2017-12-22 09:52
1306 查看
来自:http://blog.csdn.net/tracybochen/article/details/8550184
报文分发程序需提供这么一个功能: Qt对下载文件的时间修改(ftp,http下载)
当我们下载一个文件到本地后,文件的相关属性(如修改时间,创建时间,访问时间)都会修改为下载后的系统时间。
而报文分发程序中想让下载到本地的文件保留源文件的修改时间,此时我们必须手动对文件的属性就行操作。
但是Qt做不了,只能通过在Qt程序中调用系统命令。可以直接包括#include<windows.h>或者#include <sys/utime.h>然后调用系统函数。
Qt中没有相应的函数,修改文件时间这种操作是和平台相关的,所以如果程序是需要跨平台的话,需要进行条件编译。不同的平台下调用不同的系统函数进行设置。
其中涉及的API:
windows下:_utime()
linux: utime()
下面介绍实现需要的函数:
PS: 不同操作系统下实现都很类似,只是函数名变了个样。
int _utime( const char *filename, struct _utimbuf *times );//Windows
int utime ( const char *filename, struct utimbuf *times );//linux
形参介绍:
filename 文件路径
times 保存时间数值的指针
_utimbuf结构体的
(Windows)
struct _utimbuf
{
time_t actime; /* Access time */
time_t modtime; /* Modification time */
};
(other OS)
struct utimbuf
{
time_t actime;
time_t modtime;
};
tm结构体 储存时间信息
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
之间还涉及到将QString类型转换为const char*类型,作为_utime()的第一个形参。
QString str= QTextCodec::codecForLocale()->toUnicode("测试12345678");
QByteArray ary;
ary.append(str);
char *fileName = ary.data();
//4.5 修改本地temp下文件的修改时间
void ExcuThread::modifyDownFileTime(const QDateTime& dtLastModify,const QString& sFilePath)
{
qApp->processEvents();
if(sFilePath.isEmpty() || !dtLastModify.isValid() || dtLastModify.isNull())
return;
int aYear = dtLastModify.date().year() - 1900;
int aMonth = dtLastModify.date().month() - 1;
int aDay = dtLastModify.date().day();
int aHour = dtLastModify.time().hour();
int aMinute = dtLastModify.time().minute();
int aSec = dtLastModify.time().second();
qDebug()<<"时间1:"<<aYear<<aMonth<<aDay<<aHour<<aMinute<<aSec;
struct tm tma = {0};
tma.tm_year = aYear;
tma.tm_mon = aMonth;
tma.tm_mday = aDay;
tma.tm_hour = aHour;
tma.tm_min = aMinute;
tma.tm_sec = aSec;
tma.tm_isdst = 0;
#ifdef Q_WS_WIN
struct _utimbuf ut;
#else
struct utimbuf ut;
#endif
ut.modtime = mktime(&tma);
QString str= QTextCodec::codecForLocale()->toUnicode(sFilePath.toAscii());
QByteArray ary;
ary.append(str);
char *fileName = ary.data();
qDebug()<<ary<<fileName;//
#ifdef Q_WS_WIN
if( _utime(fileName, &ut ) == -1 )
#else
if( utime(fileName, &ut ) == -1 )
#endif
qDebug() << " failed" ;
else
qDebug()<< "File time modified\n";
}
报文分发程序需提供这么一个功能: Qt对下载文件的时间修改(ftp,http下载)
当我们下载一个文件到本地后,文件的相关属性(如修改时间,创建时间,访问时间)都会修改为下载后的系统时间。
而报文分发程序中想让下载到本地的文件保留源文件的修改时间,此时我们必须手动对文件的属性就行操作。
但是Qt做不了,只能通过在Qt程序中调用系统命令。可以直接包括#include<windows.h>或者#include <sys/utime.h>然后调用系统函数。
Qt中没有相应的函数,修改文件时间这种操作是和平台相关的,所以如果程序是需要跨平台的话,需要进行条件编译。不同的平台下调用不同的系统函数进行设置。
其中涉及的API:
windows下:_utime()
linux: utime()
下面介绍实现需要的函数:
PS: 不同操作系统下实现都很类似,只是函数名变了个样。
int _utime( const char *filename, struct _utimbuf *times );//Windows
int utime ( const char *filename, struct utimbuf *times );//linux
形参介绍:
filename 文件路径
times 保存时间数值的指针
_utimbuf结构体的
(Windows)
struct _utimbuf
{
time_t actime; /* Access time */
time_t modtime; /* Modification time */
};
(other OS)
struct utimbuf
{
time_t actime;
time_t modtime;
};
tm结构体 储存时间信息
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
之间还涉及到将QString类型转换为const char*类型,作为_utime()的第一个形参。
QString str= QTextCodec::codecForLocale()->toUnicode("测试12345678");
QByteArray ary;
ary.append(str);
char *fileName = ary.data();
//4.5 修改本地temp下文件的修改时间
void ExcuThread::modifyDownFileTime(const QDateTime& dtLastModify,const QString& sFilePath)
{
qApp->processEvents();
if(sFilePath.isEmpty() || !dtLastModify.isValid() || dtLastModify.isNull())
return;
int aYear = dtLastModify.date().year() - 1900;
int aMonth = dtLastModify.date().month() - 1;
int aDay = dtLastModify.date().day();
int aHour = dtLastModify.time().hour();
int aMinute = dtLastModify.time().minute();
int aSec = dtLastModify.time().second();
qDebug()<<"时间1:"<<aYear<<aMonth<<aDay<<aHour<<aMinute<<aSec;
struct tm tma = {0};
tma.tm_year = aYear;
tma.tm_mon = aMonth;
tma.tm_mday = aDay;
tma.tm_hour = aHour;
tma.tm_min = aMinute;
tma.tm_sec = aSec;
tma.tm_isdst = 0;
#ifdef Q_WS_WIN
struct _utimbuf ut;
#else
struct utimbuf ut;
#endif
ut.modtime = mktime(&tma);
QString str= QTextCodec::codecForLocale()->toUnicode(sFilePath.toAscii());
QByteArray ary;
ary.append(str);
char *fileName = ary.data();
qDebug()<<ary<<fileName;//
#ifdef Q_WS_WIN
if( _utime(fileName, &ut ) == -1 )
#else
if( utime(fileName, &ut ) == -1 )
#endif
qDebug() << " failed" ;
else
qDebug()<< "File time modified\n";
}
相关文章推荐
- 设置文件属性里的时间,包括:创建时间、访问时间、修改时间
- VC ++实现 对文件的 隐藏, 只读 ,创建时间 , 最后修改时间 ,最后访问时间 属性的修改
- VC修改文件时间属性(修改/访问时间)
- [原]VC实现修改文件创建、访问、修改时间属性
- Linux 下修改文件的访问时间和修改时间属性
- MFC中获取文件的属性(包括文件创建时间、修改时间、访问时间、文件大小等)
- 如何访问一个文件的创建、修改时间、文件属性
- VC++ 获取文件属性创建时间、修改时间和访问时间
- 为什么文件属性中的“访问时间”和“修改时间”不一致?
- 为什么文件属性中的“访问时间”和“修改时间”不一致?
- MFC中修改文件属性(包括文件创建时间、文件修改时间、文件访问时间、文件大小等)
- 修改文件(文件夹)的属性(隐藏,只读,存档,创建时间,最后访问时间,最后修改时间)
- Windows平台上修改某个文件的创建、访问、修改日期时间属性
- 为什么文件属性中的“访问时间”和“修改时间”不一致?
- VC++ 获取文件属性创建时间、修改时间和访问时间
- VC实现修改文件创建、访问、修改时间属性(转载)
- 详解win7中的文件属性“访问时间”和“修改时间”不一致的原因分析 【原来LastAccessTime早就没用了】
- VC ++实现 对文件的 隐藏, 只读 ,创建时间 , 最后修改时间 ,最后访问时间 属性的修改
- Linux 终端访问 FTP 及 上传下载 文件
- 在Linux下,一个文件也有三种时间,分别是:访问时间、修改时间、状态改动时间