Domanda

Ho cercato per 2 ore o meglio di un modo per utilizzare il metodo Read7BitEncodedInt per questo. Ho bisogno di usare in qualche modo per ridurre la mia dimensione del file (in questo caso probabilmente da 100 MB o più). Sono stato anche cercando di utilizzare il metodo ReadString dal momento che sembra di fare più o meno la stessa cosa. Ma che sembra meno appropriato e io non sono davvero sicuro che avrebbe funzionato. Se c'è qualche altra alternativa a questo che io sono a conoscenza di Sarei aperto a utilizzare anche questo.

Nella sommatoria. Come faccio a implementare il metodo Read7BitEncodedInt nel codice seguente? Inoltre, io non sono troppo sicuro che il mio metodo di Write7BitEncodedInt è corretto sia.

    public void SaveFile()
    {
        using (FileStream stream = new FileStream("C:\\A_random.txt", FileMode.Create))
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                for (int i = 0; i < typeCount.Count; i++)
                {
                    writer.Write((byte)typeCount[i]);
                    writer.Write(type[i]);
                }
                writer.Close();
            }
        }
        LoadFile();
    }

    public void LoadFile()
    {
        using (FileStream stream = new FileStream("C:\\A_random.txt", FileMode.Open))
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                int i = 0;
                while (stream.Position != stream.Length)
                {
                    int count = reader.Read7BitEncodedInt();
                    byte val = reader.ReadByte();
                    for (int ii = 0; ii < count; ii++)
                    {
                        grid[i].val1 = i;
                        grid[i].val2 = val;
                        grid[i].val3 = vect;
                        i++;
                    }
                }
                reader.Close();
            }
        }
    }
È stato utile?

Soluzione

Here is a way to do it:

public class MyBinaryReader : BinaryReader {
    public MyBinaryReader(Stream stream) : base(stream) {}
    public new int Read7BitEncodedInt() {
        return base.Read7BitEncodedInt();
    }
}

public class MyBinaryWriter : BinaryWriter {
    public MyBinaryWriter(Stream stream) : base(stream) {}
    public new void Write7BitEncodedInt(int i) {
        base.Write7BitEncodedInt(i);
    }
}

And some test code:

void Main() {
var stream = new MemoryStream();

var writer = new MyBinaryWriter(stream);    

writer.Write7BitEncodedInt(100);
writer.Write7BitEncodedInt(1000);
writer.Write7BitEncodedInt(10000);
writer.Write7BitEncodedInt(100000);
writer.Write7BitEncodedInt(1000000);
writer.Write7BitEncodedInt(-1000000);

stream.Position = 0;

var reader = new MyBinaryReader(stream);    

Debug.Assert(reader.Read7BitEncodedInt() == 100);
Debug.Assert(reader.Read7BitEncodedInt() == 1000);
Debug.Assert(reader.Read7BitEncodedInt() == 10000);
Debug.Assert(reader.Read7BitEncodedInt() == 100000);
Debug.Assert(reader.Read7BitEncodedInt() == 1000000);
Debug.Assert(reader.Read7BitEncodedInt() == -1000000);
}

Altri suggerimenti

This is a case where you have a solution to a problem in mind before you ask the question, even if the solution is a bad idea. There is a blog entry on either Raymond Chen's blog, Eric Lippert's blog or probably both about this subject, but I couldn't find it (them).

In the last 20 years, needing to care about 7-bit characters has been mostly phased out. And, the notion of trying to save disk space by using them is patently ridiculous.

The only way this would even work would be if you overlapped characters so that you effectively combined 8 characters into 7 bytes. I can almost guarantee without looking that that is not what Read7BitEncodedInt does.

(Actually, I have no idea what it does, but fortunately, I don't care)

If you need to work with large files, use compression of some kind. Zip, GZip, 7-Zip, whatever.

EDIT: Since you are not actually asking about strings, then this may be of use. YMMV, however. Look for the ReadVLI and WriteVLI functions.

http://gilgamesh.hamsterrepublic.com/websvn/filedetails.php?repname=ohrrpgce&path=%2Fwip%2Freload.bas&rev=3568&peg=4164

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top