我已经下载了.gz文件,并使用'gzip -d'成功将其解压缩。但是当我尝试使用python zlib按块将其解压缩时,出现了错误。
CHUNK = 1024 * 1024
infile = open('2019-07-06-13.log.gz')
d = zlib.decompressobj(32 + zlib.MAX_WBITS)
while True:
chunk = infile.read(CHUNK)
if not chunk:
break
data = d.decompress(chunk)
print len(chunk), len(data)
print "#####"
由于文件很小,因此此循环仅运行一次。 “ len(data)”小于“ len(chunk)”的打印结果肯定是错误的。
输出:
100576 50389
#####
同时,如前所述,我使用gzip -c重新压缩通过使用“ gzip -d”创建的解压缩文件后,我使用代码解压缩了重新压缩的文件,并且生成的镜头转向正确,这表示我的代码对于普通的gz文件效果很好。
答案 0 :(得分:2)
感谢DavisHerring的提示!。关键问题在于,原始gz文件是由多个gz子文件串联而成的,其解压缩更加复杂。
以下是解决方法:
public class BackGroundDistanceCalculate extends Service {
final public static String TAG = "BackGroundDistance";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, MapsActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
int i;
for(i=0;i<1000;i++)
{
Log.d(TAG,"Value of i is : "+ i);
SystemClock.sleep(1000);
}
Notification notification =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Calculating Distance")
.setContentText("Distance Calculation is running")
.setSmallIcon(R.drawable.ic_favorite)
.setContentIntent(pendingIntent)
.build();
//startForeground(1, notification);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 1 :(得分:0)