C# - Extract one *.jar file into another one [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to do my own Minecraft Launcher for Moded Minecraft. And I have a issue: I need to extract content of one *.jar file to another. I tried a lot of things and I am totally desperate.
Basicly, I need to do this in code.

As Andrew briefly mentioned, there are some good .NET libraries for manipulating zip files. I've found DotNetZip to be very useful, and there are lots of helpful worked examples here.
In answer to your question, if you just want to copy the contents of one *.jar file to another, keeping original content in the target file, please try the following. I'm using the .NET zip mentioned above (which also has a NuGet package!):
using (ZipFile sourceZipFile = ZipFile.Read("zip1.jar"))
using (ZipFile targetZipFile = ZipFile.Read("zip2.jar"))
{
foreach (var zipItem in sourceZipFile)
{
if (!targetZipFile.ContainsEntry(zipItem.FileName))
{
using (Stream stream = new MemoryStream())
{
// Write the contents of this zip item to a stream
zipItem.Extract(stream);
stream.Position = 0;
// Now use the contents of this stream to write to the target file
targetZipFile.AddEntry(zipItem.FileName, stream);
// Save the target file. We need to do this each time before we close
// the stream
targetZipFile.Save();
}
}
}
}
Hope this helps!

Related

Read .Exe file Using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c# How many way to open file? Which one is best? and how to open .exe file? Sorry for silly question but i am new in c#.
using (StreamReader srStreamReader = new StreamReader(sString))
{
while ((sline = srStreamReader.ReadLine()) != null)
{
Console.WriteLine(sline);
}
}
I am use this code for this but am not able. so please help
If I understood the problem correctly
You can use like this
string path;
byte[] bufferArray = File.ReadAllBytes(path);
string base64EncodedString = Convert.ToBase64String(bufferArray );
bufferArray = Convert.FromBase64String(base64EncodedString );
File.WriteAllBytes(path, bufferArray );
By open file do you mean execute it or read line by line?
If execute then probably something like this is the answer:
Process.Start("C:\\");
From the code you've provided, it looks like you want to be able to view the source of an .exe. This can't be done without using a decompiler and knowing what the application was compiled with.
If you're trying to execute the .exe file, then take a look at the static method System.Diagnostics.Process.Start(filePath).
If you're trying to actually read the contents, you can use ILSpy or other similar software to decompile the application to view source. ILSpy has source available on GitHub, so you'll be able to use that to get the contents you want.

Open Resource File with FileStream fails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to open a Resource File with the FileStream Class. It's a text File and I want to read it Line by Line.
FileStream fs = new FileStream(Properties.Resources.Testing, FileMode.Open, FileAccess.Read);
The called Exception is System.ArgumentException and it says there is an invalid character.
I hope anyone can help me to fix this, or if theres a better way it's also ok but I need the File in the .exe so it needs to be a Resource..
When you add a text file as a resource, it will be embedded as a string. So your FileStream constructor call will assume that you are trying to open a file on disk with a name that is the same as the text file content. That ends poorly of course.
It isn't very clear if you really want a stream, the string tends to be good as-is, you might consider the String.Split() method to break it up into lines. Or maybe you like the StringReader class so you can use ReadLine():
using (var rdr = new StringReader(Properties.Resources.Testing)) {
string line;
while ((line = rdr.ReadLine()) != null) {
// Do something with line
//...
}
}

Saving ArrayList values to XML C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have ArrayList nabídka, I have to save everything from this ArrayList to XML file using stream writer and on starting application load the XML file to ArrayList with stream reader. Does anybody know how to do it? (Yes, I know that I shouldn not use ArrayList, but i must complete this project with it.)
Heres an example of how it might be implemented
ArrayList sampleList = new ArrayList();
sampleList.Add(" ");
//Add your elements
//StreamWriter initialized with append mode
StreamWriter streamwriter = new StreamWriter(" INSERT PATH OF XML HERE ", true);
for (int i = 0; i < sampleList.Count; i++)
{
//Elements are written into the file here, remember not to forget the xml structure
streamwriter.WriteLine(sampleList[i]);
}
//You have to close the streamwriter or you have to flush to make sure the text is saved
streamwriter.Close();
I hope I could help you
Also possible:
var xmlSerializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(YourType) });
But for serializing it would be better to have a List<t>.
The benefit of serializing is the easy read in and that you even can share whole objects easily between languages/applications.

Line Number of String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
StreamReader reader = new StreamReader("C:\\ABC\\XYZ.txt");
I am reading a file using streamreader, the file is a HL7 file
MSH|^~\&|ABC|000|ABC|ABC|0000||ABC|000|A|00
PID|1|000|||ABC||000|A||||||||||
PV1|1|O||||||||||||||||||||||||||||||||||||||||||
OBR|1|||00||00|00|||||||||||ABC|00|0|0||||A|||||00||ABC|7ABC||ABC
OBX|1|ABC|ABC|1|SGVsbG8=
I need to find the line number of OBX, the file has character delimiters at the end of each line e.g. MSH|^~\&|ABC|000|ABC|ABC|0000||ABC|000|A|00*CR*LF
The reason I need this is that I need to get the Base64 inside the OBX field, and write it out as a file. my reader will always be a stream, I cannot use the file stream. The above code was an example, the following implementation is to be made in BizTalk and the file I will reading will be stream because that's how BizTalk allows me to access the information in of my file.
var lineNum = File.ReadLines(fname)
.Select((s, line) => new { s, line })
.First(x => x.s.StartsWith("OBX|"))
.line;

Write to txt in a 'form-like' manner [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm implementing a Windows application to make planning projects easier at my workplace and I was wondering if there's any clever way of making a txt-file nicely structured.
The application is really very simple, pretty much what it does is give the user a question which is answered in a textbox bellow. The question AND the answer are then both sent to a file but it looks very tacky.
Example:
Question?Answer!Question?Answer!
I would like it to be more like this:
Question?
Answer!
Question?
Answer!
I was also curious about other types files, is it possible to use Pdf or MS word the same way as txt?
You can use File.AppendAllLines() and pass in the different strings as an array. They will appear as separate lines in the text file. You'll also need to add a using System.IO at the top of your file.
For example:
// Ensure file exists before we write
if (!File.Exists("<YOUR_FILE_PATH>.txt"))
{
using (File.CreateText("<YOUR_FILE_PATH>.txt")) {}
}
File.AppendAllLines("<YOUR_FILE_PATH>.txt", new string[] {
"Question1",
"Answer1",
"Question2",
"Answer2",
"Question3",
"Answer3"
});
I hope this is what you're after - the question is a little vague.
As for Word and PDF files, this is more complex. Here's a link to a StackOverflow question about Word:
How can a Word document be created in C#?
and one about PDF:
Creating pdf files at runtime in c#
For a simple text file you could use
StringBuilder fileData = new StringBuilder();
fileData.AppendLine("Question: blah! blah! blah! blah!");
fileData.AppendLine("Answer: blah! blah! blah! blah!");
FileStream fs = new FileStream("yourFile.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(fileData.ToString());
sw.Flush();
fs.Flush();
fs.Close();
But of course it won't give you that bold question flavor, for that you would have to use something else,
like MS Word interop, to learn that visit here.
I wanted to mention the String.Format function.
suppose, you have your strings question and answer, you could do some
using (var stream = new StreamWriter('myfile.txt', true)) // append=true
stream.Write(String.Format("\n{0}\n{1}\n\n{2}\n",
question,
new String('=',question.Length),
answer);
to get a text file like
Question 1
==========
Answer
Second Question
===============
Answer
You might also want to use String.Trim() on question to get rid of leading and trailing whitespace (question = question.Trim()), so the "underline" effect looks nicely.

Categories