I want to create a zip file in Xamarin Forms Cross Platform.
I use a custom way for every platform, iOS and Android.
In iOS works with the Library ZipArchive, but I not found alternative for Android.
So I try do it native (to create zip with only one file), but the zip file was created empty.
public void Compress(string path, string filename, string zipname)
{
var personalpath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string folder = Path.Combine(personalpath, path);
string zippath = Path.Combine(folder, zipname);
string filepath = Path.Combine(folder, filename);
System.IO.FileStream fos = new System.IO.FileStream(zippath, FileMode.OpenOrCreate);
Java.Util.Zip.ZipOutputStream zos = new Java.Util.Zip.ZipOutputStream(fos);
ZipEntry entry = new ZipEntry(filename.Substring(filename.LastIndexOf("/") + 1));
byte[] fileContents = File.ReadAllBytes(filepath);
zos.Write(fileContents);
zos.CloseEntry();
}
Solution by Leo Nix and OP.
Need to close ZOS.
fos and zos should be disposed.
...
zos.CloseEntry();
zos.Close();
zos.Dispose();
fos.Dispose();
}
I've noticed the ask and solution code wasn't complete. I had to change somethings to make it work, so here is the complete code:
public void ZipFile(string fullZipFileName, params string[] fullFileName)
{
using (FileStream fs = new FileStream(fullZipFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (ZipOutputStream zs = new ZipOutputStream(fs))
{
foreach (var file in fullFileName)
{
string fileName = Path.GetFileName(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zs.PutNextEntry(zipEntry);
byte[] fileContent = System.IO.File.ReadAllBytes(file);
zs.Write(fileContent);
zs.CloseEntry();
}
zs.Close();
}
fs.Close();
}
}
I hope it help.
Related
Trying to create a Flutter plugin that copies an asset file to the native Application Documents Folder.
For iOS, I achieved this by the following code (see below).
However, since I do not have much knowledge of the Android architecture, I would like to know how my Android MethodChannel code should look like.
My Android part of this Flutter plugin needs to be in KOTLIN !
I need a file copy from the Android assets folder to the Documents Folder of Android - all this done inside the Flutter plugin and in Kotlin!
Again, I have iOS in Swift ready made. What is missing is the Android in Kotlin counter part. Do you have any help on this ?
.
Here is the working code for the iOS FlutterMethodChannel in Swift:
(i.e. it copies a file from the main-bundle to the Documents-Directory of the iPhone...)
import UIKit
private func copyFile(fileName: String) -> String {
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return "Could not find documents URL"
}
let finalURL = documentsUrl.first!.appendingPathComponent(fileName)
if !( (try? finalURL.checkResourceIsReachable()) ?? false) {
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent(fileName)
do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalURL.path)
return "\(finalURL.path)"
} catch let error as NSError {
return "Couldn't copy file to final location! Error:\(error.description)"
}
} else {
return "\(finalURL.path)"
}
}
In Kotlin, I tried this - but it does not work at all....:(
import java.io.File
private fun copyFileTrial1(fileName: String): String {
File src = new File("../../assets/${fileName}");
File dst = new File("../../DocumentsFolder/${fileName}", src.getName());
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
return "hello1"
}
Or I tried this - but again - completely without success :(
private fun copyFileTrial2(fileName: String): String {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fileName);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(outDir, filenfileNameame);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + fileName, e);
}
return "hello2"
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
I have finally found a solution to file copy issue in Kotlin !
It was especially helpful in achieving my very first Flutter plugin.
Here is the solution of the file-copy in Kotlin
import java.io.File
import java.io.InputStream
import io.flutter.util.PathUtils
private fun copyFile(fileName: String): String {
val assetStream: InputStream = mRegistrar.context().assets.open(fileName)
val appliationDocumentsFolderPath: String = PathUtils.getDataDirectory(mRegistrar.context())
val outputFilePath: String = appliationDocumentsFolderPath + "/" + fileName
if (!File(outputFilePath).exists()) {
File(outputFilePath).copyInputStreamToFile(assetStream)
}
return outputFilePath
}
private fun File.copyInputStreamToFile(inputStream: InputStream) {
inputStream.use { input ->
this.outputStream().use { fileOut ->
input.copyTo(fileOut)
}
}
}
I am making a soundboard for practice and I want to give the user the ability to download the sound (that I have included in the app in the res/raw folder) onClick of a menu item but I can only find information about downloading from an internet url, not something that I already included in the apk.
What is the best way to do this? I would like to give them the option to save to an SD card also if this is possible. A point towards the correct class to use in the documentation would be great! I've been googling to no avail.
Thanks!
Try something like this:
public void saveResourceToFile() {
InputStream in = null;
FileOutputStream fout = null;
try {
in = getResources().openRawResource(R.raw.test);
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = "myfile.mp3"
fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
I don't know about the raw but I did a similar thing in my app using the assets folder. My files are under the assets/backgrounds folder as you can probably guess from the code below.
You can modify this code and make it work for you (I know I will only have 4 files which is why I have i go from 0 to 4 but you can change this to whatever you want).
This code copies the file starting with prefix_ (like prefix_1.png, prefix_2.png, etc) to my cache directory but you can obviously change the extension, the filename or the path you would like to save the assets to.
public static void copyAssets(final Context context, final String prefix) {
for (Integer i = 0; i < 4; i++) {
String filename = prefix + "_" + i.toString() + ".png";
File f = new File(context.getCacheDir() + "/" + filename);
if (f.exists()) {
f.delete();
}
if (!f.exists())
try {
InputStream is = context.getAssets().open("backgrounds/" + filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) {
Log.e("Exception occurred while trying to load file from assets.", e.getMessage());
}
}
}
I have seen an example about this question.But when I have a try on it , it just occur an error in this code File f = new File(Uri.parse(uri.toString())); and just say that Constructor File(Uri) is undefined . I have stucked in this error for many days. I don't know what's wrong with it because other people can work. The following is the suggested code
public class WiFiDirectBundle extends Serializable {
private String fileName;
private String mimeType;
private Long fileSize;
private byte[] fileContent;
public WiFiDirectBundle() {}
// adds a file to the bundle, given its URI
public void setFile(Uri uri) {
File f = new File(Uri.parse(uri.toString()));
fileName = f.getName();
mimeType = MimeTypeMap.getFileExtensionFromUrl(f.getAbsolutePath());
fileSize = f.length();
FileInputStream fin = new FileInputStream(f);
fileContent = new byte[(int) f.length()];
fin.read(fileContent);
}
// restores the file of the bundle, given its directory (change to whatever
// fits you better)
public String restoreFile(String baseDir) {
File f = new File(baseDir + "/" + fileName);
try {
FileOutputStream fos = new FileOutputStream(f);
if (fileContent != null) {
fos.write(fileContent);
}
fos.close();
return f.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getFileName() {
return fileName;
}
public String getMimeType() {
return mimeType;
}
public Long getFileSize() {
return fileSize;
}
}
And this is original qusetion:
How to find file name of a file which is transferred via wifi direct mode in android?
Also I am a beginner in Wifi Direct ,I can successfully link two device and transfer file ,but I want to linked more device and transfer file Sequentially can anyone give me some advices about learning it or some examples about how to do it.Thanks!
use
File f = new File(uri.getPath());
instead of
File f = new File(Uri.parse(uri.toString()));
to get file path from Uri
you can open inputstream of a file from its uri using content resolver
ContentResolver cr=getContentResolver();
InputStream in=cr.OpenInputStream(file_uri);
you can use this input stream to read from the file
I'm trying to make an Android application that can open a docx file to read, edit and save it.
My idea is to extract all the xml file within the archive to a temp folder. In this folder we can edit the content of the docx in /word/document.xml. The problem is when I compress this temp folder to make a new docx file and replace the old file, inside the new docx archive the path is like /mnt/sdcard/temp/"all files xml go here" while the xml files should be in the first level.
Can anybody help me to go through this? here is the method to compress the temp directory
Note: dir2zip argument's value I use is /mnt/sdcard/temp/***.docx
public void zipDir(String dir2zip, ZipOutputStream zos)
{
try
{
//create a new File object based on the directory we
//have to zip File
File zipDir = new File(dir2zip);
//get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
//loop through dirList, and zip the files
for(int i=0; i<dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if(f.isDirectory())
{
//if the File object is a directory, call this
//function again to add its content recursively
String filePath = f.getPath();
zipDir(filePath, zos);
//loop again
continue;
}
//if we reached here, the File object f was not a directory
//create a FileInputStream on top of f
FileInputStream fis = new FileInputStream(f);
//create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath());
//place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
//now write the content of the file to the ZipOutputStream
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
//close the Stream
fis.close();
}
}
catch(Exception e)
{
//handle exception
}
}
I have managed to fix it by myself. The problem is in this line:
File f = new File(zipDir, dirList[i]);
It should be
File f = new File(dirList[i]);
If the argument zipDir is included, the absolute path to the directory will be used in the archive!
I have now managed to get the original poster's code working on Mac and Windows by making the following two modifications:
1: add a ZipEntry for each directory: do not simply ignore it
2: remove the directory name from the ZipEntry name
Note: zipinfo is useful
This is a program that works for me:
import java.io.*;
import java.util.zip.*;
public class zipdoc
{
String savedDir = null;
public void zipDir(String dir2zip, ZipOutputStream zos)
{
try
{
if (savedDir == null)
savedDir = dir2zip;
// create a new File object based on the directory we
// have to zip File
File zipDir = new File(dir2zip);
//get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
// loop through dirList, and zip the files
for (int i=0; i<dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if (f.isDirectory())
{
// if the File object is a directory, call this
// function again to add its content recursively
System.out.println("Adding dir: " + f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath().substring(savedDir.length()+1) + "/");
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
String filePath = f.getPath();
zipDir(filePath, zos);
// loop again
continue;
}
else if (!f.getName().equals(".DS_Store"))
{
// if we reached here, the File object f was not a directory
// and it's not the MacOSX special .DS_Store
// create a FileInputStream on top of f
System.out.println("Adding file: " + f);
FileInputStream fis = new FileInputStream(f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath().substring(savedDir.length()+1));
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
// now write the content of the file to the ZipOutputStream
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
// close the Stream
fis.close();
}
}
}
catch(Exception e)
{
// handle exception
System.out.println(e);
}
}
public void zipit(String inDir, String outFile)
{
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(outFile)));
zos.setMethod(0);
zos.setMethod(ZipOutputStream.DEFLATED);
zos.setLevel(0);
zipDir(inDir, zos);
zos.finish();
zos.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void main (String args[]) {
zipdoc z1 = new zipdoc();
// Check there are sufficient params if desired
// first param is directory to be 'zipped', second is resulting
// filename (??.docx)
// eg java zipdoc dir1 newDoc.docx
z1.zipit(args[0], args[1]);
System.out.println("Finished creating " + args[1]);
}
}
please somebody tell me what is the folder of sdcard and how can i create files in it.because i am new to android and i have googled so much but could not find any comprehensive stuff.i want to create file in sdcard manually. please help.
here is my code i have written but now it says fileNotFoundException. hence i have created a file in sdcard but still it is not recognisizing the file.any suggestions please.
try
{
String root = android.os.Environment.getExternalStorageDirectory().getPath();
File gpxfile = new File(root, "sijjeel.txt");
//FileWriter writer = new FileWriter(gpxfile);
FileOutputStream writer = new FileOutputStream(gpxfile);
writer.write(bArray, 0, bArray.length);
writer.flush();
writer.close();
}
thanks alot
Path to sdcard is:
android.os.Environment.getExternalStorageDirectory().getPath()
To write a file, you can use the regular java.io.File methods for that.
For example, for creating a text files I use a helper method like this:
/**
* Stores text content into a file
* #param filename Path to the output file
* #param content Content to be stored in file
* #throws IOException
*/
public void storeFile(final String filename, final String content, String charSet)
throws IOException {
if (charSet==null) charSet = "utf-8";
Writer w = new OutputStreamWriter( new FileOutputStream(filename), charSet );
w.write(content);
w.flush();
w.close();
}
public void storeFile(final String filename, final String content)
throws IOException {
storeFile(filename, content, null);
}
or copying a file to sdcard:
public static final void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied to " + f2.getAbsolutePath());
} catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
} catch(IOException e){
System.out.println(e.getMessage());
}
}
It's /sdcard (if you actually have a card in it)
The code you provided will create the file if it is not already there. Make sure you run your program on the emulator that has an SD card mounted. If it doesn't you can see an icon in the notification area saying so.