c#解压缩错误:存档条目是使用不受支持的压缩方法进行压缩的。

huangapple go评论131阅读模式
英文:

c# decompression error The archive entry was compressed using an unsupported compression method

问题

class Program {
    static void Main (string[] args) {
          
        String rawString = "6979414848205548481619299442879901900893978332594614407044767717485407280104077714658698163325401659212830920734233047578454701810567032015270223682917915825234703754712504887921309181789607809168884583848396456653007022479356336240198130363930881632367124738541517499494458139647378808680614169273221404741476596583953169248831376224396335169577064812987140578144885819479190173537644970232125142253963784979138011318798385442436099901621998283624816070080504830712594525760596934341576755626791590403636878139861665599383319429228364434183913197958738697001410493839281298692342829951566712530309758759364649701153639921979798429707566199261950037418171329283207372048014948669160666776198414040633384677104717697507521717586776709084200364956178863636105988867260929887577092955570407803783021397897341999914616790441029837229129746669225095633201097644321593502503404440714110515167034889128258965583435965030225845348564582051521348800742574442877087774194668983516629631073341202705453382780613775427336949283388084891654484225446940941660942440637784744293259916479841407088189462964489670231866481904237338494872813098890875845640034370370387108798950180220865436012752487216677041817312930119747601017807577565413977545693375480131324240696099879479436722576566447939593195590684591261809038023122178172006150499569185218838749337238281597037288924464009997530938336798176023597292328320965086990184531426188862965408313308973495924965144113396593829090645266653313774582036138982013368561474719154447134894466611560589758251829063226370300282175823479569847261439348404558251402273730865053482214589180028302043821438357583302818374143973997002745047526405755760407045006694423501337081780299815080324840337828812644300041900356816429114261098230198976752026002079876882796597235615015594486182057781476152918170746403157005216896239428521706033466061587608065036133153074432195952131368564234168005447770190345777024917629879639171161719929852078265309160759260989590618158889891835294735614366674503961584445497685736312628248483551986529867423016255476553691922054241686230968975229511700928171281549902682365302333677412951788839806869796040512235899311734337858684531156721416280114473368826463098485252394260075790386415875290922570568686439586036262465414002334117870088922801660529414759784318799843806130096998190881240404138869293309782335305296720666220243304175086358278211355789957998014801209332293458940463859106591986434520433810583569309224929264228263841477378949329312443958215939294432669464260216534074560882723006838459792812340253078330291135526952675203790833430237852831740601433198364243363569730205351077393441691141240055900819091229931605146865520183001810239708464322588389956036291760175558843819105418234580239610174323636606095262722940143706063698846499673285377621180570537788160304936809915237889489342387891057012783726694920184573202789672963922380028271124448024265644396686341508447830351380242127542393849410283830409594988503246799544444687606954881510597515686410993828907588979699141180160893062603338104857903239845856783130275935413569275439908789983311663211937449259444259898972766208";

        var data = Decompress(rawString);
        Console.WriteLine(Encoding.UTF8.GetString(data));
        Console.ReadLine();
    }

    static byte[] Decompress(string rawString) {
        var _bi = BigInteger.Parse(rawString);
        var _bary = _bi.ToByteArray();

        var ms = new MemoryStream(_bary, true);

        using (var zipStream = new GZipStream(ms, CompressionMode.Decompress)) {
            const int size = 1024;
            byte[] buffer = new byte[size];
            using (MemoryStream memory = new MemoryStream()) {
                int count = 0;
                do {
                    count = zipStream.Read(buffer, 0, size);
                    if (count > 0) {
                        memory.Write(buffer, 0, count);
                    }
                } while (count > 0);
                return memory.ToArray();
            }
        }
    }
}
英文:

I am trying to create a c# version of this repo https://github.com/dimagi/AadharUID which is written for Android.
The below class is mocking https://github.com/dimagi/AadharUID/blob/master/app/src/main/java/org/commcarehq/aadharuid/ScanResult.java#L244

Iam getting error "The archive entry was compressed using an unsupported compression method"

What is wrong with my code?

NB : rawString is taken from https://github.com/dimagi/AadharUID/blob/master/app/src/test/java/org/commcarehq/aadharuid/ScanResultTest.java#L180

    class Program {
    static void Main (string[] args) {
      
        String rawString = "6979414848205548481619299442879901900893978332594614407044767717485407280104077714658698163325401659212830920734233047578454701810567032015270223682917915825234703754712504887921309181789607809168884583848396456653007022479356336240198130363930881632367124738541517499494458139647378808680614169273221404741476596583953169248831376224396335169577064812987140578144885819479190173537644970232125142253963784979138011318798385442436099901621998283624816070080504830712594525760596934341576755626791590403636878139861665599383319429228364434183913197958738697001410493839281298692342829951566712530309758759364649701153639921979798429707566199261950037418171329283207372048014948669160666776198414040633384677104717697507521717586776709084200364956178863636105988867260929887577092955570407803783021397897341999914616790441029837229129746669225095633201097644321593502503404440714110515167034889128258965583435965030225845348564582051521348800742574442877087774194668983516629631073341202705453382780613775427336949283388084891654484225446940941660942440637784744293259916479841407088189462964489670231866481904237338494872813098890875845640034370370387108798950180220865436012752487216677041817312930119747601017807577565413977545693375480131324240696099879479436722576566447939593195590684591261809038023122178172006150499569185218838749337238281597037288924464009997530938336798176023597292328320965086990184531426188862965408313308973495924965144113396593829090645266653313774582036138982013368561474719154447134894466611560589758251829063226370300282175823479569847261439348404558251402273730865053482214589180028302043821438357583302818374143973997002745047526405755760407045006694423501337081780299815080324840337828812644300041900356816429114261098230198976752026002079876882796597235615015594486182057781476152918170746403157005216896239428521706033466061587608065036133153074432195952131368564234168005447770190345777024917629879639171161719929852078265309160759260989590618158889891835294735614366674503961584445497685736312628248483551986529867423016255476553691922054241686230968975229511700928171281549902682365302333677412951788839806869796040512235899311734337858684531156721416280114473368826463098485252394260075790386415875290922570568686439586036262465414002334117870088922801660529414759784318799843806130096998190881240404138869293309782335305296720666220243304175086358278211355789957998014801209332293458940463859106591986434520433810583569309224929264228263841477378949329312443958215939294432669464260216534074560882723006838459792812340253078330291135526952675203790833430237852831740601433198364243363569730205351077393441691141240055900819091229931605146865520183001810239708464322588389956036291760175558843819105418234580239610174323636606095262722940143706063698846499673285377621180570537788160304936809915237889489342387891057012783726694920184573202789672963922380028271124448024265644396686341508447830351380242127542393849410283830409594988503246799544444687606954881510597515686410993828907588979699141180160893062603338104857903239845856783130275935413569275439908789983311663211937449259444259898972766208";

        var data = Decompress (rawString);
        Console.WriteLine (Encoding.UTF8.GetString (data));
        Console.ReadLine ();
    }
    static byte[] Decompress (string rawString) {

        // parsing string to BigInteger
        var _bi = BigInteger.Parse (rawString);
        // convert to ByteArray
        var _bary = _bi.ToByteArray ();

        var ms = new MemoryStream (_bary, true);

        // inflate
        using (var zipStream = new GZipStream (ms, CompressionMode.Decompress)) {
            {
                const int size = 1024;
                byte[] buffer = new byte[size];
                using (MemoryStream memory = new MemoryStream ()) {
                    int count = 0;
                    do {
                        count = zipStream.Read (buffer, 0, size);
                        if (count > 0) {
                            memory.Write (buffer, 0, count);
                        }
                    }
                    while (count > 0);
                    return memory.ToArray ();
                }
            }
        }
    }

}

答案1

得分: 2

当在Java上运行时,(new BigInteger(rawString).toByteArray()) 返回如下内容:

1F8B080000000000000033FA6F6169606164606869 . . . . . DA5FE98B4C050000

这个二进制数据具有有效的gzip头部(1f 8b - 魔术字节,08 deflate 等等)

然而,在.NET中,BigInteger.Parse(rawString).ToByteArray() 返回如下内容:

0000054C8BE95FDA . . . . . 08 8B 1F

你看出了什么吗?

是的,它是Java生成的字节的反转。这是因为Java和.NET在写入这些字节时使用了不同的字节序。

只需要这样做:

// 将字符串解析为BigInteger
var _bi = BigInteger.Parse(rawString);
// 转换为字节数组
var _bary = _bi.ToByteArray();

// 给我们一个神奇的帮助!
Array.Reverse(_bary);
英文:

When run on Java, the (new BigInteger(rawString).toByteArray()) returns this:

1F8B080000000000000033FA6F6169606164606869 . . . . . DA5FE98B4C050000

This binary data has a valid gzip header (1f 8b - magic bytes, 08 deflate etc..)

However, the conversion in .Net, BigInteger.Parse(rawString).ToByteArray() returns this:

0000054C8BE95FDA . . . . . 08 8B 1F

Do you see something?

Yes, It is the reverse of the java produced bytes. This is because java and .net are using different endiannes when writing these bytes.

Just do this:

// parsing string to BigInteger
var _bi = BigInteger.Parse(rawString);
// convert to ByteArray
var _bary = _bi.ToByteArray();

// Give us a magic hand!
Array.Reverse(_bary);

huangapple
  • 本文由 发表于 2020年4月7日 00:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定