File types and compression question

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2182 Views - Last Post: 22 March 2013 - 07:38 AM Rate Topic: -----

#1 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

File types and compression question

Posted 20 March 2013 - 03:57 PM

I am having an issue with a compression program I am creating. Firstly the program as of right now only copies the file and writes to a new file with a custom file type (not yet developed). The only files that work are txt and similar files. I cannot copy an exe file or .lnk or basically anything other than txt files. I don't know much about file types and how they work, though that's why I'm doing this project, to learn.

My question is this: What would I need to do to be able to copy different file types properly without corruption?

Example:

private void CreateFile(string read, string write)
        {
            TextReader tr = new StreamReader(read);
            TextWriter tw = new StreamWriter(write);
            string line = "";
            while (line != null)
            {
                line = tr.ReadLine();
                tw.WriteLine(line);
            }
            tw.Close();
            tr.Close();
        }

What I have now makes a 109MB executable into a 198MB executable and is corrupted. I might be going in the wrong direction here, but that's what I have so far (as far as actual I/O). I want to at least be able to copy a file without error (or at least a very small margin of error, nothing is error free) before I continue. Any suggestions? If needed I can post my full code, but I'll wait for someone to ask. Any help is appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: File types and compression question

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: File types and compression question

Posted 20 March 2013 - 04:01 PM

Why don't you use an established compression routine? If you don't know much about file types then you really aren't qualified to try to invent your own compression routine. What's wrong with all the existing compression schemes that you can't use .zip for example?

Judging by your CreateFile routine you are WAY off. TextWriter will make a text file. But .exe are more than just text. They are binary data. You would need a binary writer.

Again, you are re-inventing the wheel. Just use established .zip routines.
Was This Post Helpful? 1
  • +
  • -

#3 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 20 March 2013 - 04:12 PM

Well the main purpose of this is for me to learn about file types and how they work. Also I just read about binary writer and I think I'll try that. As for re-inventing the wheel or being qualified, I know I'm not but what other way is there to learn? If I use some other routine already packaged that I just drop in what fun is that for me? I can take a look at it and figure out how they do certain things I suppose, but I'd rather learn myself. I just needed a point in the right direction and possibly more information on how file types work. Thanks for your suggestions though :)/>/>

This post has been edited by GunnerInc: 21 March 2013 - 02:53 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: File types and compression question

Posted 20 March 2013 - 04:27 PM

You learn by reading. Then you gain experience by doing.

If you want to learn how to design a house you don't order a bunch of wood and start cutting and hammering with no plan.

There is SO much information out there about file formats that just sitting down and banging on the keyboard in an effort to stumble across some understanding is nearly pointless. Dissecting known-good code is a valid learning methodology. Its why people write tutorials: To teach the next generation and provide code examples they EXPECT to be read and dissected for deeper comprehension.

Plus you are talking about 'compression' but there is no compression in your CreateFile() method. Are you simply trying to 'combine' not 'compress'?
Was This Post Helpful? 0
  • +
  • -

#5 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 20 March 2013 - 04:46 PM

Yeah I'm trying to compress the files eventually, that's the goal of my program. Something similar to 7-zip. I'm not sure what you mean by "'combine' not 'compress'". I've been reading tutorials mostly just the ones regarding file types and how to read/write files.

I'm now about to make a binary reader/writer part of my code and seeing if that works, just got done making the function. For now all I want to be able to do is copy the file without error, then work my way up to eventually compressing using a file format of my own. That part I will have a lot of trouble with.

I just wanted advice about the compression part, but the problem I was having was the corrupted files other than .txt. I'll look up some tutorials about compression and look at some code and dissect it, I thought maybe it wasn't too difficult but I see that I may even need to learn some assembly.

This post has been edited by GunnerInc: 21 March 2013 - 02:53 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: File types and compression question

Posted 20 March 2013 - 05:00 PM

Quote

I'm not sure what you mean by "'combine' not 'compress'".

Well...
  • Combining would be taking 10 files of 1 meg each and getting a single file of 10 megs. The results are merely a combined form of the source.
  • Compressing would be taking 10 files of 1 meg ecah and getting a single file of LESS THAN 10 megs. The results are a compressed form of the original.



Quote

Yeah I'm trying to compress the files eventually, that's the goal of my program. Something similar to 7-zip.

Have you read anything on compression schemes and theory? Do you comprehend how a jpg is compressed? Do you understand how the .zip technique works to compress then later inflate data? Data compression techniques tend to be quite advanced, require a low-level understanding of file structures and data structures and involve high-end mathematics.
http://www.google.co...=UTF-8&oe=UTF-8
Was This Post Helpful? 1
  • +
  • -

#7 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 20 March 2013 - 07:59 PM

Quote

  • Combining would be taking 10 files of 1 meg each and getting a single file of 10 megs. The results are merely a combined form of the source.
  • Compressing would be taking 10 files of 1 meg ecah and getting a single file of LESS THAN 10 megs. The results are a compressed form of the original.


Oh ok, I understand now. Compression is what I'm aiming for.

Quote

Have you read anything on compression schemes and theory? Do you comprehend how a jpg is compressed? Do you understand how the .zip technique works to compress then later inflate data? Data compression techniques tend to be quite advanced, require a low-level understanding of file structures and data structures and involve high-end mathematics.
http://www.google.co...=UTF-8&oe=UTF-8


I've read some on compression methods, LZMA, etc. Yes I understand that it's going to be very advanced and use high-end mathematics. I have time to learn all that. I really want to get to know how to do this proficiently. If it means learning Calculus, etc. That's OK.

Also, I fixed the corrupted files problem (at least all the files I've tested (.exe, .lnk, .jpg) worked perfectly fine.

private void CreateFile(string read, string write)
        {
            switch (this.filetype)
            {
                case ".txt":
                    {
                        TextReader tr = new StreamReader(read);
                        TextWriter tw = new StreamWriter(write);
                        string line = "";
                        while (line != null)
                        {
                            line = tr.ReadLine();
                            tw.WriteLine(line);
                        }
                        tw.Close();
                        tr.Close();
                        return;
                    }
                default:
                    {
                        FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
                        FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                        BinaryReader r = new BinaryReader(fsr);
                        BinaryWriter w = new BinaryWriter(fsw);
                        int pos = 0;
                        int length = (int)r.BaseStream.Length;
                        while (pos < length)
                        {
                            int line = r.ReadInt32();
                            w.Write(line);
                            pos += sizeof(int);
                        }
                        w.Close();
                        r.Close();
                        fsw.Close();
                        fsr.Close();
                        return;
                    }
            }
        }


Thanks for the suggestions. I'll take a look at some source code, even though it's hard to understand some of it. I know that I'm undertaking a huge project; it's something that I've wanted to do for a while but just never had time. Now I have loads of time so why not?

This post has been edited by kiasta: 20 March 2013 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,082
  • Joined: 05-May 12

Re: File types and compression question

Posted 20 March 2013 - 09:18 PM

Why are you treating copying text files differently? Be warned that the StreamWriter, can, and sometimes will output data that is different from the data that you thought you read it. This is because the StreamReader will decode a file and read it into a canonical UTF-16 formatted string. The StreamWriter is free to encode the text data in that format. For example if your text file has multiple Unicode characters that allow combining like A with combining E, then the StreamWriter can potentially save out the two incoming characters as a single .

For most file compression algorithms, the requirement is that must be lossless compression algorithms. If you allow the combining to happen, then you've just lost data (although retained information).
Was This Post Helpful? 0
  • +
  • -

#9 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 21 March 2013 - 03:33 AM

Yeah, I modified my code and I've yet to start adding compression algorithms until I get copying working perfectly, at least.

private void CreateFile(string read, string write)
        {
            try
            {
                FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
                FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                BinaryReader r = new BinaryReader(fsr);
                BinaryWriter w = new BinaryWriter(fsw);
                int pos = 0;
                int length = (int)r.BaseStream.Length;
                while (pos < length)
                {
                    int line = r.ReadInt32();
                    w.Write(line);
                    pos += sizeof(int);
                }
                w.Close();
                r.Close();
                fsw.Close();
                fsr.Close();
            }
            catch (FileNotFoundException FileEx)
            {
                MessageBox.Show(FileEx.Message);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


My problem now is when I try to copy txt files and some other files I get this error:
Unable to read beyond the end of the stream
I've tried a couple different things like changing this:
int length = (int)r.BaseStream.Length;
to this:
int length = (int)r.BaseStream.Length / sizeof(int);
As was suggested by several places, even from stack overflow. However, all of my copies are now corrupted. So I went another way and changed this:
int line = r.ReadInt32();
to this:
int line = r.ReadInt16();
And changed
int length = (int)r.BaseStream.Length / sizeof(int);
back to what it was before, but still the same issue, the copies are corrupted. Now, I want to start compressing but if I can't make copies properly there is no point. For now I'm a bit stuck and google is not much help...

This post has been edited by GunnerInc: 21 March 2013 - 02:53 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,082
  • Joined: 05-May 12

Re: File types and compression question

Posted 21 March 2013 - 05:27 AM

What's wrong with ReadByte()? The file length is given to you in bytes, why are you trying to read the file an integer at a time?
http://msdn.microsof...m.readbyte.aspx
http://msdn.microsof...r.readbyte.aspx

Or better yet as you get into dictionary based compression, and move away from Huffman based compression, you'll need to fill a buffer so the Read() methods that fill a byte array would be a better approach.

In the "old days" reading a byte a time was frowned upon because of the overhead of making each I/O call. Nowadays, people seem to just overlook it with the assumption that the OS will do the correct buffering for you and read the data in chunks in the background anyways.
Was This Post Helpful? 1
  • +
  • -

#11 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 21 March 2013 - 07:35 AM

I've tried ReadByte() but the copies become corrupted. I've tried several different things: Changing from int to Int32, Byte, Int16, Int64 and not just the ReadXXX() but the data types of the variables inside the function as well, for instance:

private void CreateFile(string read, string write)
        {
            try
            {
                FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
                FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                BinaryReader r = new BinaryReader(fsr);
                BinaryWriter w = new BinaryWriter(fsw);
                Byte pos = 0;
                Byte length = (Byte)fsr.Length;
                while (pos < length)
                {
                    Byte line = r.ReadByte();
                    w.Write(line);
                    pos += sizeof(Byte);
                }
                w.Close();
                r.Close();
                fsw.Close();
                fsr.Close();
            }
            catch (FileNotFoundException FileEx)
            {
                MessageBox.Show(FileEx.Message);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


With that I get weird sizes and they are all corrupted. Using int seems to have the right size like so:

private void CreateFile(string read, string write)
        {
            try
            {
                FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
                FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                BinaryReader r = new BinaryReader(fsr);
                BinaryWriter w = new BinaryWriter(fsw);
                int pos = 0;
                int length = (int)fsr.Length;
                while (pos < length)
                {
                    int line = r.ReadByte();
                    w.Write(line);
                    pos += sizeof(int);
                }
                w.Close();
                r.Close();
                fsw.Close();
                fsr.Close();
            }
            catch (FileNotFoundException FileEx)
            {
                MessageBox.Show(FileEx.Message);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


but the files are all corrupted, and when using ReadInt32() some files get the stream error error and hang on the file, creating a 0 byte file but others works fine and are not corrupted. Here is my full code if you wish to look at it, though what I've posted already is the main topic and problem, the rest is mostly design.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Compression_Util
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Design());
        }
    }
}


Compression_Util.Designer.cs

namespace Compression_Util
{
    partial class Design
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.DropBox = new System.Windows.Forms.Panel();
            this.DropLabel = new System.Windows.Forms.Label();
            this.DragLabel = new System.Windows.Forms.Label();
            this.DropBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // DropBox
            // 
            this.DropBox.AllowDrop = true;
            this.DropBox.Controls.Add(this.DropLabel);
            this.DropBox.Controls.Add(this.DragLabel);
            this.DropBox.Location = new System.Drawing.Point(12, 12);
            this.DropBox.Name = "DropBox";
            this.DropBox.Size = new System.Drawing.Size(307, 277);
            this.DropBox.TabIndex = 0;
            this.DropBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.DropBox_DragDrop);
            this.DropBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.DropBox_DragEnter);
            this.DropBox.DragLeave += new System.EventHandler(this.DropBox_DragLeave);
            // 
            // DropLabel
            // 
            this.DropLabel.AutoSize = true;
            this.DropLabel.Location = new System.Drawing.Point(123, 131);
            this.DropLabel.Name = "DropLabel";
            this.DropLabel.Size = new System.Drawing.Size(61, 15);
            this.DropLabel.TabIndex = 2;
            this.DropLabel.Text = "Drop Here";
            this.DropLabel.Visible = false;
            // 
            // DragLabel
            // 
            this.DragLabel.AllowDrop = true;
            this.DragLabel.AutoSize = true;
            this.DragLabel.Location = new System.Drawing.Point(78, 131);
            this.DragLabel.Name = "DragLabel";
            this.DragLabel.Size = new System.Drawing.Size(151, 15);
            this.DragLabel.TabIndex = 1;
            this.DragLabel.Text = "Drag File Here to Compress";
            // 
            // Main
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.ClientSize = new System.Drawing.Size(331, 301);
            this.Controls.Add(this.DropBox);
            this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Main";
            this.Text = "Main";
            this.DropBox.ResumeLayout(false);
            this.DropBox.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        public System.Windows.Forms.Panel DropBox;
        public System.Windows.Forms.Label DropLabel;
        public System.Windows.Forms.Label DragLabel;



    }
}




Compression_Util.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Compression_Util
{
    public partial class Design : Form
    {
        public Design()
        {
            InitializeComponent();
        }

        private void DropBox_DragDrop(object sender, DragEventArgs e)
        {
            DropBox db = new DropBox(sender, e, this, "Drop");
        }

        private void DropBox_DragEnter(object sender, DragEventArgs e)
        {
            DropBox db = new DropBox(sender, e, this, "Enter");
        }

        private void DropBox_DragLeave(object sender, EventArgs e)
        {
            DropBox db = new DropBox(sender, e, this, "Leave");
        }
    }
}



DropBox.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Compression_Util
{
    class DropBox
    {
        private object sender;
        private EventArgs e;
        private DragEventArgs de;
        private Design zip;
        private string hover;
        private string[] files;
        private string filetype;

        public DropBox(object sender, DragEventArgs de, Design zip, string hover)
        {
            this.sender = sender;
            this.de = de;
            this.zip = zip;
            this.hover = hover;
            this.Hover();
        }
        public DropBox(object sender, EventArgs e, Design zip, string hover)
        {
            this.sender = sender;
            this.e = e;
            this.zip = zip;
            this.hover = hover;
            this.Hover();
        }
        private void CreateFile(string read, string write)
        {
            try
            {
                FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
                FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                BinaryReader r = new BinaryReader(fsr);
                BinaryWriter w = new BinaryWriter(fsw);
                int pos = 0;
                int length = (int)fsr.Length;
                while (pos < length)
                {
                    int line = r.ReadInt32();
                    w.Write(line);
                    pos += sizeof(int);
                }
                w.Close();
                r.Close();
                fsw.Close();
                fsr.Close();
            }
            catch (FileNotFoundException FileEx)
            {
                MessageBox.Show(FileEx.Message);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private string CreateFileName(string file)
        {
            int i = 0;
            string deleteText = "";
            string newFileName = "";
            while ((i = file.IndexOf('.', i)) != -1)
            {
                deleteText = file.Substring(i);
                i++;
            }
            this.filetype = deleteText;
            //MessageBox.Show(deleteText);
            newFileName = file.Replace(deleteText, ".dlz");
            return newFileName;
        }
        private void Hover()
        {
            switch (this.hover)
            {
                case "Enter":
                    {
                        zip.DragLabel.Visible = false;
                        zip.DropLabel.Visible = true;
                        zip.BackColor = System.Drawing.Color.DarkGray;
                        zip.DropBox.BackColor = System.Drawing.Color.Thistle;
                        if (de.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                        {
                            de.Effect = DragDropEffects.All;
                        }
                        break;
                    }
                case "Leave":
                    {
                        zip.DragLabel.Visible = true;
                        zip.DropLabel.Visible = false;
                        zip.BackColor = System.Drawing.Color.WhiteSmoke;
                        zip.DropBox.BackColor = System.Drawing.Color.WhiteSmoke;
                        break;
                    }
                case "Drop":
                    {
                        zip.DragLabel.Visible = true;
                        zip.DropLabel.Visible = false;
                        zip.BackColor = System.Drawing.Color.WhiteSmoke;
                        zip.DropBox.BackColor = System.Drawing.Color.WhiteSmoke;
                        this.Drop();
                        break;
                    }
            }
        }
        private void Drop()
        {
            this.files = (string[])this.de.Data.GetData(DataFormats.FileDrop);

            foreach (string file in files)
            {
                //MessageBox.Show(file);
                string newFileName = this.CreateFileName(file);
                //MessageBox.Show(newFileName);
                if (!File.Exists(newFileName))
                {
                    this.CreateFile(file, newFileName);
                }
                else
                {
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;
                    string message = "File already exists, Overwrite?";
                    string caption = "";
                    result = MessageBox.Show(zip, message, caption, buttons);
                    switch (result)
                    {
                        case DialogResult.Yes:
                            {
                                this.CreateFile(file, newFileName);
                                break;
                            }
                        case DialogResult.No:
                            {
                                MessageBox.Show("Operation has been cancelled!");
                                break;
                            }
                        default: break;
                    }
                }
            }
        }
    }
}



As you can see, it's not doing much. Only copying for now. I'll check those links, and I'll start into lossless compression. But I just wanted to get this working first before even tackling the compression part. Maybe I'm going about it all wrong and the method I'm doing is pointless I guess I should do more research.

This post has been edited by GunnerInc: 21 March 2013 - 02:52 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#12 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: File types and compression question

Posted 21 March 2013 - 09:50 AM

According to your post it isn't doing anything at all, much less copying. You will have to use ReadByte() so I suggest you figure out what the problem is. Short copy code I wrote had no issues with reading/writing files.
Was This Post Helpful? 0
  • +
  • -

#13 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 21 March 2013 - 11:20 AM

It creates a file in the directory of the file that's been dropped onto the window. It does something, nothing really useful yet, but it's something. I got ReadByte() to work, I just had to change some data types:

while (pos < length)
                {
                    Byte line = r.ReadByte();
                    w.Write(line);
                    pos += sizeof(Byte);
                }


This post has been edited by GunnerInc: 21 March 2013 - 02:52 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,082
  • Joined: 05-May 12

Re: File types and compression question

Posted 21 March 2013 - 12:28 PM

The OP's problem is because of:
Byte length = (Byte)fsr.Length;



He is truncating the file size to whatever length fits in a byte.
Was This Post Helpful? 0
  • +
  • -

#15 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: File types and compression question

Posted 21 March 2013 - 01:42 PM

Here is what I have finally:

private void CreateFile(string read, string write)
{
    try
    {
        FileStream fsr = new FileStream(read, FileMode.Open, FileAccess.Read);
        FileStream fsw = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
        BinaryReader r = new BinaryReader(fsr);
        BinaryWriter w = new BinaryWriter(fsw);
        int pos = 0;
        int length = (int)r.BaseStream.Length;
        while (pos < length)
        {
            Byte line = r.ReadByte();
            w.Write(line);
            pos += sizeof(Byte);
        }
        w.Close();
        r.Close();
        fsw.Close();
        fsr.Close();
    }
    catch (FileNotFoundException FileEx)
    {
        MessageBox.Show(FileEx.Message);
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message);
    }
}



It seems to be working properly, though I'm not entirely sure if that is the right way since all the tutorials I've seen online are not very elaborate. They only show a simple demonstration using a loop to iterate through and write the iterator into the file.

Now I can focus on the compression part at least. Though it looks very complicated and It's a bit over my head. I'm just going to spend a couple of days reading and studying the LZMA lossless compression method, hopefully I can write something that works.

This post has been edited by GunnerInc: 21 March 2013 - 02:52 PM
Reason for edit:: Remvoed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2