|
|
(Chilkat for Android™ API) Controlling Paths of Files Added to Zip
When appending files to a .zip, it can be difficult to get the paths exactly as you want them. This example describes a way that you can control the path information when adding a single file at a time to a .zip.
Download: Chilkat for Android™ Java Libraries
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String outStr = "";
CkZip zip = new CkZip();
boolean success;
success = zip.UnlockComponent("anything for 30-day trial");
if (success != true) {
outStr += "Failed to unlock zip component" + "\n";
tv.setText(outStr);
setContentView(tv);
return;
}
// Intialize the zip object by calling NewZip.
zip.NewZip("myZip.zip");
// Add a reference to a file on disk to the zip object.
// (When appending files to a zip, the files are not actually
// read into memory. References to the file(s) are added.
// When WriteZip is called, the referenced files are streamed in
// and compressed to the .zip.)
boolean saveExtraPath;
saveExtraPath = false;
success = zip.AppendOneFileOrDir("/temp/a/hamlet.xml",saveExtraPath);
// For brevity, this example will not check the return status...
// The saveExtraPath argument, if set, will cause the path information
// passed in the 1st argument to be saved in the .zip.
// To add "a/hamlet.xml", do this:
zip.put_AppendFromDir("/temp");
saveExtraPath = true;
success = zip.AppendOneFileOrDir("a/hamlet.xml",saveExtraPath);
// To add "temp/a/hamlet.xml", do this:
zip.put_AppendFromDir("/");
saveExtraPath = true;
success = zip.AppendOneFileOrDir("temp/a/hamlet.xml",saveExtraPath);
// The zip written here will contain hamlet.xml three times,
// each with a different relative path.
success = zip.WriteZipAndClose();
tv.setText(outStr);
setContentView(tv);
}
static {
// Important: Make sure the name passed to loadLibrary matches the shared library
// found in your project's libs/armeabi directory.
// for "libchilkat.so", pass "chilkat" to loadLibrary
// for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
// etc.
//
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}
|