我试图使用国家气象服务(美国)数据,但最近发生了一些变化,GZip文件不再打开。
.NET 4.5抱怨......
extension UIPasteboard {
func arrayForPasteboardType(pasteboardType: String) -> NSArray? {
switch valueForPasteboardType(pasteboardType) {
case let array as NSArray:
return array
case let data as NSData:
if let array = NSPropertyListSerialization.propertyListWithData(data, options: 0, format: nil, error: nil) as? NSArray {
return array
}
default:
break
}
return nil
}
}
我不明白发生了什么变化,但这已经成为一个真正的表演者。任何有GZip格式经验的人都可以告诉我发生了什么变化让这个停止工作吗?
有效的文件:
无法正常工作的文件:
Message=The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
Source=System
StackTrace:
at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
at System.IO.Compression.Inflater.Decode()
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
答案 0 :(得分:5)
看来此服务SOMETIMES返回伪装成tar
的{{1}}格式文件。这非常令人困惑,但如果您检查前两个字节是.tar.gz
和0x1F
,您可以通过手动检查其幻数来检测文件是否为GZip。
0x8B
答案 1 :(得分:-4)
[已解决] GZipStream抱怨标题中的幻数不正确
//异常幻数tar.gz文件
Migcal错误原因
解决方案
使用.net framework 4.5.1来解决此异常// OR //
在不改变.net框架的情况下完成现有解决方案。 请按照实施步骤进行操作。
Remane abc.tar.gz as abc(删除扩展名)。
传递此文件并直接命名为压缩功能
* public static void Compress(DirectoryInfo directorySelected,string directoryPath)
{
foreach (FileInfo fileToCompress in directorySelected.GetFiles())
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".tar.gz")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".tar.gz"))
{
using (System.IO.Compression.GZipStream compressionStream = new System.IO.Compression.GZipStream(compressedFileStream,
System.IO.Compression.CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
FileInfo info = new FileInfo(directoryPath + "\\" + fileToCompress.Name + ".tar.gz");
}
}
}
}
3. implement this code in following exception handler try catch block
try
{
TarGzFilePath=@"c:\temp\abc.tar.gz";
FileStream streams = File.OpenRead(TarGzFilePath);
string FileName=string.Empty;
GZipInputStream tarGz = new GZipInputStream(streams);
TarInputStream tar = new TarInputStream(tarGz);
// exception will occured in below lines should apply try catch
TarEntry ze;
try
{
ze = tar.GetNextEntry();// exception occured here "magical number"
}
catch (Exception extra)
{
tar.Close();
tarGz.Close();
streams.Close();
//please close all above , other wise it will come with exception "tihs process use by another process"
//rename your file *for better accuracy you can copy file to other location
File.Move(@"c:\temp\abc.tar.gz", @"c:\temp\abc"); // rename file
DirectoryInfo directorySelected = new DirectoryInfo(Path.GetDirectoryName(@"c:\temp\abc"));
Compress(directorySelected, directoryPath); // directorySelected=c:\temp\abc , directoryPath=c:\temp\abc.tar.gz // process in step 2 function
streams = File.OpenRead(TarGzFilePath);
tarGz = new GZipInputStream(streams);
tar = new TarInputStream(tarGz);
ze = tar.GetNextEntry();
}
// do anything with extraction with your code
}
catch (exception ex)
{
tar.Close();
tarGz.Close();
streams.Close();
}