コード例 #1
0
ファイル: zip_ReadTests.cs プロジェクト: yang73137/corefx
        public static async Task ReadInterleaved()
        {
            using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"))))
            {
                ZipArchiveEntry e1 = archive.GetEntry("first.txt");
                ZipArchiveEntry e2 = archive.GetEntry("notempty/second.txt");

                //read all of e1 and e2's contents
                Byte[] e1readnormal  = new Byte[e1.Length];
                Byte[] e2readnormal  = new Byte[e2.Length];
                Byte[] e1interleaved = new Byte[e1.Length];
                Byte[] e2interleaved = new Byte[e2.Length];

                using (Stream e1s = e1.Open())
                {
                    ZipTest.ReadBytes(e1s, e1readnormal, e1.Length);
                }
                using (Stream e2s = e2.Open())
                {
                    ZipTest.ReadBytes(e2s, e2readnormal, e2.Length);
                }

                //now read interleaved, assume we are working with < 4gb files
                const Int32 bytesAtATime = 15;

                using (Stream e1s = e1.Open(), e2s = e2.Open())
                {
                    Int32 e1pos = 0;
                    Int32 e2pos = 0;

                    while (e1pos < e1.Length || e2pos < e2.Length)
                    {
                        if (e1pos < e1.Length)
                        {
                            Int32 e1bytesRead = e1s.Read(e1interleaved, e1pos,
                                                         bytesAtATime + e1pos > e1.Length ? (Int32)e1.Length - e1pos : bytesAtATime);
                            e1pos += e1bytesRead;
                        }

                        if (e2pos < e2.Length)
                        {
                            Int32 e2bytesRead = e2s.Read(e2interleaved, e2pos,
                                                         bytesAtATime + e2pos > e2.Length ? (Int32)e2.Length - e2pos : bytesAtATime);
                            e2pos += e2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1interleaved, e1readnormal.Length);
                ZipTest.ArraysEqual <Byte>(e2readnormal, e2interleaved, e2readnormal.Length);

                //now read one entry interleaved
                Byte[] e1selfInterleaved1 = new Byte[e1.Length];
                Byte[] e1selfInterleaved2 = new Byte[e2.Length];


                using (Stream s1 = e1.Open(), s2 = e1.Open())
                {
                    Int32 s1pos = 0;
                    Int32 s2pos = 0;

                    while (s1pos < e1.Length || s2pos < e1.Length)
                    {
                        if (s1pos < e1.Length)
                        {
                            Int32 s1bytesRead = s1.Read(e1interleaved, s1pos,
                                                        bytesAtATime + s1pos > e1.Length ? (Int32)e1.Length - s1pos : bytesAtATime);
                            s1pos += s1bytesRead;
                        }

                        if (s2pos < e1.Length)
                        {
                            Int32 s2bytesRead = s2.Read(e2interleaved, s2pos,
                                                        bytesAtATime + s2pos > e1.Length ? (Int32)e1.Length - s2pos : bytesAtATime);
                            s2pos += s2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1selfInterleaved1, e1readnormal.Length);
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1selfInterleaved2, e1readnormal.Length);
            }
        }